using
System;
using
System.Collections.Generic;
using
System.DirectoryServices;

namespace
Tool

...
{
public class IISHelper

...{

-- 字段 --#region -- 字段 --
private string _server; //服务器名或IP
private int _port; // 端口
private int _siteID; //站点ID
private string _serverComment; //站点描述
private string _localPath; //站点本地路径
private List<string> _domain; //站点绑定域名
private int _minSiteID; //站点ID最小值
private int _maxSiteID;//站点ID最大值
private bool _createIdBySort;//是否按照顺序产生站点
private string _defaultDoc;
#endregion


-- 构造函数 --#region -- 构造函数 --
public IISHelper()

...{
_server = "localhost";
_siteID = 1;
_port = 80;
_minSiteID = 2;
_maxSiteID = 100000;
_createIdBySort = false;
_defaultDoc = "default.html,default.htm,index.html,index.htm,default.aspx";
}
public IISHelper(string server)

...{
_server = server;
_siteID = 1;
_port = 80;
_minSiteID = 2;
_maxSiteID = 100000;
_createIdBySort = false;
_defaultDoc = "default.html,default.htm,index.html,index.htm,default.aspx";
}

#endregion


-- 属性 --#region -- 属性 --
public string Server

...{

get ...{ return _server; }

set ...{ _server = value; }
}
public int Port

...{

get ...{ return _port; }

set ...{ _port = value; }
}
public int SiteID

...{

get ...{ return _siteID; }

set ...{ _siteID = value; }
}
public string ServerComment

...{

get ...{ return _serverComment; }

set ...{ _serverComment = value; }
}
public int MaxSiteID

...{

get ...{ return _maxSiteID; }

set ...{ _maxSiteID = value; }
}
public int MinSiteID

...{

get ...{ return _minSiteID; }

set ...{ _minSiteID = value; }
}
public List<string> DoMain

...{

get ...{ return _domain; }

set ...{ _domain = value; }
}
public string LocalPath

...{

get ...{ return _localPath; }

set ...{ _localPath = value; }
}
public bool CreateIdBySort

...{

get ...{ return _createIdBySort; }

set ...{ _createIdBySort = value; }
}
public string DefaultDoc

...{

get ...{ return _defaultDoc; }

set ...{ _defaultDoc = value; }
}
#endregion


-- 公开方法 --#region -- 公开方法 --
public void CreateSite()

...{
// get id
if (_createIdBySort)

...{
_siteID = GetNewSiteIDBySort();
}
else

...{
_siteID = GetNewSiteID();
}

// check site
if (!ChkSiteUnique(_domain))

...{
throw new Exception("same site has existed");
}
else

...{
DirectoryEntry root = new DirectoryEntry("IIS://" + _server + "/W3SVC");
DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", _siteID);

for (int index = 0; index < _domain.Count; index++) // 绑定多个域名

...{
if (_server == "localhost")

...{
site.Properties["ServerBindings"].Add(":" + _port.ToString() + ":" + _domain[index]);
}
else

...{
site.Properties["ServerBindings"].Add(_server + ":" + _port.ToString() + ":" + _domain[index]);
}
}

site.Invoke("Put", "ServerComment", _serverComment);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", _defaultDoc);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");

DirectoryEntry siteVirtualDir = site.Children.Add("Root", "IISWebVirtualDir");
siteVirtualDir.Properties["Path"][0] = _localPath;
siteVirtualDir.Properties["AccessFlags"][0] = 512;
siteVirtualDir.Properties["FrontPageWeb"][0] = 1;
siteVirtualDir.Properties["AppRoot"][0] = "LM/W3SVC/" + _siteID.ToString() + "/Root";
siteVirtualDir.Properties["AppFriendlyName"][0] = "ROOT";

siteVirtualDir.CommitChanges();
site.CommitChanges();

siteVirtualDir.Close();
site.Close();
}

}
public void DeleteSiteByName(string siteName)

...{
int m_siteID = GetSiteNumberByName(siteName);
DirectoryEntry entry = new DirectoryEntry();
entry.Path = String.Format("IIS://{0}/w3svc/{1}", _server, m_siteID);
DirectoryEntry root = new DirectoryEntry();
root.Path = String.Format("IIS://{0}/w3svc", _server);

root.Children.Remove(entry);
root.CommitChanges();

entry.Close();
root.Close();
}
public void DeleteSiteById(int siteID)

...{
DirectoryEntry entry = new DirectoryEntry();
entry.Path = String.Format("IIS://{0}/w3svc/{1}", _server, siteID);
DirectoryEntry root = new DirectoryEntry();
root.Path = String.Format("IIS://{0}/w3svc", _server);

root.Children.Remove(entry);
root.CommitChanges();

entry.Close();
root.Close();
}
public void StopSiteById(int siteID)

...{
using (DirectoryEntry site = new DirectoryEntry())

...{
site.Path = String.Format("IIS://{0}/W3SVC/{1}", _server, siteID);
site.Invoke("Stop", "");
}
}
public void StartSiteById(int siteID)

...{
using (DirectoryEntry site = new DirectoryEntry())

...{
site.Path = String.Format("IIS://{0}/W3SVC/{1}", _server, siteID);
site.Invoke("Start", "");
}
}
public void PauseSiteById(int siteID)

...{
using (DirectoryEntry site = new DirectoryEntry())

...{
site.Path = String.Format("IIS://{0}/W3SVC/{1}", _server, siteID);
site.Invoke("Pause", "");
}
}
public void ContinueSiteById(int siteID)

...{
using (DirectoryEntry site = new DirectoryEntry())

...{
site.Path = String.Format("IIS://{0}/W3SVC/{1}", _server, siteID);
site.Invoke("Continue", "");
}
}

public int GetSiteNumberByName(string siteName)

...{
int id = 0;
DirectoryEntry root = new DirectoryEntry();
root.Path = String.Format("IIS://{0}/W3SVC", _server);

foreach (DirectoryEntry son in root.Children)

...{
if (son.SchemaClassName == "IIsWebServer" && son.Properties["ServerComment"].Value.ToString() == siteName)

...{
id = Int32.Parse(son.Name);
son.Close();
root.Close();
return id;
}
}
root.Close();
return id;
}
#endregion

private int GetNewSiteID()

...{
Random r = new Random((int)DateTime.Now.Ticks);
int newID = r.Next(_minSiteID, _maxSiteID);
while (!ChkIDUnique(newID)) //get unique id

...{
newID = r.Next(_minSiteID, _maxSiteID);
}
return newID;
}
private int GetNewSiteIDBySort()

...{
int newID = _minSiteID;
while (!ChkIDUnique(newID)) //get unique id

...{
newID++;
if (newID >= _maxSiteID)

...{
throw new Exception("id overflowed");
}
}
return newID;
}
private bool ChkIDUnique(int siteID)

...{
bool flag = true;
DirectoryEntry root = new DirectoryEntry();
root.Path = String.Format("IIS://{0}/W3SVC", _server);

foreach (DirectoryEntry son in root.Children)

...{
if (son.SchemaClassName == "IIsWebServer")

...{
if (siteID == Int32.Parse(son.Name.Trim()))

...{
flag = false;
}
}
}
return flag;
}
private bool ChkSiteUnique(List<string> domains)

...{
DirectoryEntry root = new DirectoryEntry();
root.Path = String.Format("IIS://{0}/W3SVC", _server);
foreach (DirectoryEntry son in root.Children)

...{
if (son.SchemaClassName == "IIsWebServer" && son.Properties["ServerBindings"].Value != null)

...{
for (int i = 0; i < domains.Count; i++)

...{
for (int j = 0; j < son.Properties["ServerBindings"].Count; j++)

...{
string serverBindingsStr;
if (_server == "localhost")

...{
serverBindingsStr = ":" + _port.ToString() + ":" + domains[i];
}
else

...{
serverBindingsStr = _server + ":" + _port.ToString() + ":" + domains[i];
}

if (serverBindingsStr == son.Properties["ServerBindings"][j].ToString())

...{
return false;
}
}
}
}
}
return true;
}
}
}
大概写了一下,用来做过两个WEB 应用的安装包,发现还可以完善
现在有一个问题就是当系统上有两个freamwork版本如1.1 2.0的时候,无法设置站点使其使用正确的版本,哪位兄台知道告诉我一下,谢谢了:)
下面是使用例子
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Configuration.Install;

namespace
InstallHelper

...
{
[RunInstaller(true)]
public partial class InstallHelper : Installer

...{
public InstallHelper()

...{
InitializeComponent();
}

public override void Install(System.Collections.IDictionary stateSaver)

...{
base.Install(stateSaver);


string _serverComment = this.Context.Parameters["serverComment"];
string _server = this.Context.Parameters["server"];
string _port = this.Context.Parameters["port"];
string _domain = this.Context.Parameters["domain"];
string _localPath = this.Context.Parameters["targetdir"];

Tool.IISHelper iis = new Tool.IISHelper();
iis.Server = _server;
List<string> domains = new List<string>();
domains.Add(_domain);
iis.DoMain = domains;
iis.Port = Int32.Parse(_port);
iis.ServerComment = _serverComment;
iis.LocalPath = _localPath;
iis.CreateSite();

stateSaver.Add("siteID", iis.SiteID);
}

public override void Uninstall(System.Collections.IDictionary savedState)

...{
base.Uninstall(savedState);
if (savedState.Count != 0)

...{
if (savedState.Contains("siteID"))

...{
int siteID = Int32.Parse(savedState["siteID"].ToString());
Tool.IISHelper iis = new Tool.IISHelper();
iis.DeleteSiteById(siteID);
}
}

}

}
}