DirectoryEntry ent;
DirectoryEntry rootEntry;
try
{
ent = EnsureNewWebSiteAvailable(host + ":" + port + ":" + webSiteDesc);
if (ent != null)
{
//这里如果用户输入的网站在IIS中已经存在,那么直接获取网站的root对象,也就是网站的默认应用程序
rootEntry = ent.Children.Find("root", "IIsWebVirtualDir");
}
else
{
//如果网站在IIS不存在,那么我们需要首先在IIS中创建该网站,并且为该网站创建一个root应用程序
string entPath = string.Format("IIS://{0}/w3svc", Host);
DirectoryEntry root = GetDirectoryEntry(entPath);
string newSiteNum = GetNewWebSiteID();
DirectoryEntry newSiteEntry = root.Children.Add(newSiteNum, "IIsWebServer");
newSiteEntry.CommitChanges();
newSiteEntry.Properties["ServerBindings"].Value = host + ":" + port + ":" + webSiteDesc;
newSiteEntry.Properties["ServerComment"].Value = webSiteComment;
newSiteEntry.CommitChanges();
rootEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
rootEntry.CommitChanges();
rootEntry.Properties["Path"].Value = webSitePath;
rootEntry.Properties["AppPoolId"].Value = appPool;
rootEntry.Properties["AccessRead"][0] = true; // 勾选读取
rootEntry.Properties["AuthFlags"][0] = 1+4;
//勾选匿名访问和windows身份验证
rootEntry.Properties["DontLog"][0] = true;
rootEntry.Properties["AuthAnonymous"][0] = true;
rootEntry.Properties["AnonymousUserName"][0] =
XmlSettings.GetWebXmlSettingString("IISAnonymousUserName");
rootEntry.Invoke("AppCreate", true);
rootEntry.CommitChanges();
}
DirectoryEntry de = rootEntry.Children.Add(friendlyName, rootEntry.SchemaClassName);
de.CommitChanges();
de.Properties["Path"].Value = virtualPath;
de.Properties["AccessRead"][0] = true; // 勾选读取
de.Invoke("AppCreate", true);
de.Properties["EnableDefaultDoc"][0] = true;
de.Properties["AccessScript"][0] = true; // 脚本资源访问
de.Properties["DontLog"][0] = true; // 勾选记录访问
de.Properties["ContentIndexed"][0] = true; // 勾选索引资源
de.Properties["AppFriendlyName"][0] = friendlyName; //应用程序名
de.Properties["AuthFlags"][0] = 5;
de.CommitChanges();
}
catch (Exception e)
{
throw e;
}
public string GetNewWebSiteID()
{
ArrayList list = new ArrayList();
string tempStr;
string entPath = string.Format("IIS://{0}/w3svc",Host);
DirectoryEntry ent = GetDirectoryEntry(entPath);
foreach (DirectoryEntry child in ent.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
tempStr = child.Name.ToString();
list.Add(Convert.ToInt32(tempStr));
}
}
list.Sort();
var newId = Convert.ToInt32(list[list.Count - 1]) + 1;
return newId.ToString();
}
public DirectoryEntry GetDirectoryEntry(string entPath)
{
DirectoryEntry ent;
if (string.IsNullOrEmpty(UserName))
{
ent = new DirectoryEntry(entPath);
}
else
{
ent = new DirectoryEntry(entPath, Host + "\" + UserName, Password, AuthenticationTypes.Secure);
}
return ent;
}
public DirectoryEntry EnsureNewWebSiteAvailable(string bindStr)
{
string entPath = string.Format("IIS://{0}/w3svc",Host);
DirectoryEntry ent = GetDirectoryEntry(entPath);
foreach (DirectoryEntry child in ent.Children)
{
if (child.SchemaClassName == "IIsWebServer")
{
if (child.Properties["ServerBindings"].Value != null)
{
if (child.Properties["ServerBindings"].Value.ToString() == bindStr)
{ return child; }
}
}
}
return null;
}