用ADSI控制IIS创建网站, 虚拟目录…… (C#)

 这是当年赶工的网站用到的东东, 结果快做完了警察叔叔来个网站要备案,朋友不干了,我也停工了,

翻出来共享了
~

据说,控制IIS和别的MS的垃圾(MS的FTP, 用户管理..)可以用ADSI和WMI(WIN2k3的才好),

参考了网友们的资料,有版权问题麻烦EMAIL一下

原来是按三层写的代码没有整理, 权当笔记,省点稿纸, 大家看个大概, 详细的MSDN都有
!

三个文件:

////FileName: hostservice.cs

//


using  System;
using  System.Data;

using  Wooyea.WebSite.Modules.Hosts.DataAccess;
using  Wooyea.WebSite.Modules.Hosts.Configuration;

namespace  Wooyea.WebSite.Modules.Hosts.Business
{
 
/// <summary>
 
/// Summary description for HostService.
 
/// </summary>

 public class HostService
 
{
  
Private fields

  
Properties

  
public HostService()
  
{
   Configuration.ModuleConfig config 
= new ModuleConfig();
   settings 
= config.GetSettings();
  }


  
public HostService(string name, string description, decimal price, string ip, string rootPath) : this()
  
{
   
this.name   = name;
   
this.description = description;
   
this.price   = price;
   
this.ip    = ip;
   
this.rootPath  = rootPath;

  }


  
public HostService(int id) : this()
  
{
   
this.id = id;
   GetDetails();
  }


  
public void GetDetails()
  
{
   DataAccess.HostServices dataHostServices 
= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices(settings.ConnectionString);

   DataRow tempRow 
= dataHostServices.GetDetails(id);

   
this.name  = (string)tempRow["HostServiceName"];
   
this.price  = Convert.ToDecimal(tempRow["HostServicePrice"]);
   
this.ip   = (string)tempRow["HostServiceIP"];
   
this.port  = (int)tempRow["HostServicePort"];
   
this.rootPath = (string)tempRow["HostServiceRootPath"];

  }

  
  
public DataSet GetHostServices()
  
{
   DataAccess.HostServices dataHostServices
= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices(settings.ConnectionString);

   
return dataHostServices.GetHostServices();
  }


  
public int Create()
  
{
   DataAccess.HostServices dataHostServices
= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices(settings.ConnectionString);

   
return dataHostServices.Insert(name, description, price, rootPath, ip);
  }


  
public bool Update()
  
{
   DataAccess.HostServices dataHostServices
= new Wooyea.WebSite.Modules.Hosts.DataAccess.HostServices(settings.ConnectionString);

   
return dataHostServices.Update(id, name, description, price, rootPath, ip);
  }


 }

}

end hostservice.cs/



//  FileName: IISManager.cs

using  System;
using  System.Collections;
using  System.Text.RegularExpressions;
using  System.Text;
using  System.DirectoryServices;

using  Wooyea.WebSite.Modules.Hosts;

namespace  Wooyea.WebSite.Modules.Hosts.Business
{
 
/// <summary>
 
/// Summary description for Class1.
 
/// </summary>

 class IISManager
 
{

  
public IISManager()
  
{}

  
public DirectoryEntry GetDirectoryEntry(string entryPath)
  
{
   
// creater direntry instance depend on local or remote
   DirectoryEntry dirEntry = new DirectoryEntry(entryPath);

   
return dirEntry;
  }


  
public bool CreateSite(WebSiteInfo newSiteInfo)
  
{


   
string entPath = "IIS://localhost/w3svc";
   DirectoryEntry rootEntry 
= GetDirectoryEntry(entPath);

   
string newSiteID = GetNewSiteID();

   DirectoryEntry newSiteEntry 
= rootEntry.Children.Add(newSiteID, "IIsWebServer");
   newSiteEntry.CommitChanges();

   newSiteEntry.Properties[
"ServerBindings"].Value = newSiteInfo.ServerBindings;
   newSiteEntry.Properties[
"ServerComment"].Value = newSiteInfo.Comment;
   newSiteEntry.CommitChanges();

   DirectoryEntry vDirEntry 
= newSiteEntry.Children.Add("root""IIsWebVirtualDir");
   vDirEntry.CommitChanges();

   vDirEntry.Properties[
"Path"].Value = newSiteInfo.Path;        //ph patth in disk
   vDirEntry.CommitChanges();

   
return true;

  }


  
/// <summary>
  
/// Get and return a new website ID of specify host 
  
/// </summary>
  
/// <returns>the smallest new website ID of the host</returns>

  public string GetNewSiteID()
  
{
   ArrayList idList 
= new ArrayList();
   
string tmpStr;

   
string entryPath = "IIS://localhost/W3SVC";
   DirectoryEntry entry 
= GetDirectoryEntry(entryPath);
   
   
foreach (DirectoryEntry child in entry.Children)
   
{
    
if (child.SchemaClassName == "IIsWebServer")
    
{
     tmpStr 
= child.Name.ToString();
     idList.Add(Convert.ToInt32(tmpStr));
    }

   }


   idList.Sort();

   
int i = 1;
   
foreach (int id in idList)
   
{
    
if (i == id)
    
{
     i
++;
    }

   }


   
return i.ToString();
  }



 }

}



///end IISmanager/////


// File Name:  WebSite.cs


using  System;
using  System.DirectoryServices;

using  Wooyea.WebSite.Modules.Hosts.Configuration;

using  Wooyea.WebSite.Modules.Hosts.Business;
using  Wooyea.WebSite.Modules.Hosts.DataAccess;

namespace  Wooyea.WebSite.Modules.Hosts.Business
{
 
/// <summary>
 
/// Summary description for WebSite.
 
/// </summary>

 public class WebSite
 
{
  
Private fields 
  
  
Properties

  WebSite()
  
{
   Configuration.ModuleConfig config 
= new ModuleConfig();
   settings 
= config.GetSettings();
  }


  
public WebSite(string newHeader, string newComment, string newIP, int newPort, string newPath) : this()
  
{
   
this.siteInfo.Header = newHeader;
   
this.siteInfo.IP = newIP;
   
this.siteInfo.Port = newPort;
   
this.siteInfo.Comment = newComment;
   
this.siteInfo.Path = newPath;
  }

  
/// <summary>
  
/// Create a new data record in website table and another one in the cross table 
  
/// </summary>
  
/// <param name="userID">userID of current priciple</param>
  
/// <returns></returns>

  public int CreateSite(int userID)
  
{


   IISManager iis 
= new IISManager();
   iis.CreateSite(siteInfo);

   DataAccess.WebSites newSite 
= new WebSites(settings.ConnectionString);
  
   
return newSite.Add(siteInfo, userID);

  }


 }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值