使用ASP.NET创建IIS站点或虚拟目录(下)
///<summary>
/// Delete the virtual directory in the WebServer
///</summary>
///<param name="website">webserver name</param>
///<param name="vdir">virtual directory name</param>
public static void DeleteVirtualDir(string website, string vdir)
{
if (!GetVirtualDir(website, vdir)) throw new Exception(" The virtual directory don't exist in the website");
using (DirectoryEntry de = GetWebSiteInfo(website))
{
foreach (DirectoryEntry sub in de.Children)
{
if (sub.Name == vdir)
{
de.Invoke("Delete", new string[] { sub.SchemaClassName, vdir });
de.CommitChanges();
}
}
}
}
private static void UpdateVDirInfo(DirectoryEntry newDE, ref System.Data.PropertyCollection properties)
{
//newDE.Properties["AnonyMousUserName"][0] = properties["AnonymousUserName"].ToString();
//newDE.Properties["AnonymousUserPass"][0] = properties["AnonymousUserPass"].ToString();
newDE.Properties["AccessRead"][0] = (bool)properties["AccessRead"];
newDE.Properties["AccessExecute"][0] = (bool)properties["AccessExecute"];
newDE.Properties["AuthBasic"][0] = (bool)properties["AuthBasic"];
newDE.Properties["AuthNTLM"][0] = (bool)properties["AuthNTLM"];
newDE.Properties["ContentIndexed"][0] = (bool)properties["ContentIndexed"];
newDE.Properties["EnableDefaultDoc"][0] = (bool)properties["EnableDefaultDoc"];
newDE.Properties["EnableDirBrowsing"][0] = (bool)properties["EnableDirBrowsing"];
newDE.Properties["AccessSSL"][0] = (bool)properties["AccessSSL"];
newDE.Properties["AccessScript"][0] = (bool)properties["AccessScript"];
newDE.Properties["DefaultDoc"][0] = properties["DefaultDoc"].ToString();
newDE.Properties["Path"][0] = properties["Path"].ToString();
newDE.Properties["AppIsolated"][0] = (int)properties["AppIsolated"];
newDE.Properties["AppFriendlyName"][0] = properties["AppFriendlyName"].ToString();
newDE.Properties["AccessFlags"][0] = (int)properties["AccessFlags"];
newDE.Properties["FrontPageWeb"][0] = (int)properties["FrontPageWeb"];
//newDE.Properties["DontLog"][0] = (bool)properties["DontLog"];
//newDE.Properties["AppRoot"][0] = properties["AppRoot"].ToString();
}
private static bool GetVirtualDir(string webSite, string dirName)
{
bool result = false;
using (DirectoryEntry de = GetWebSiteInfo(webSite))
{
if (de != null)
{
foreach (DirectoryEntry subVD in de.Children)
{
if (subVD.SchemaClassName == "IIsWebVirtualDir" && subVD.Name == dirName)
{
result = true;
break;
}
}
}
}
return result;
}
private static void GetWebSiteInfo(ref Hashtable webServer)
{
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer")
{
webServer.Add(subDE.Properties["ServerComment"].Value.ToString(), subDE.Name);
}
}
des = null;
}
private static DirectoryEntry GetWebSiteInfo(string website)
{
DirectoryEntry result = null;
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer" && subDE.Properties["ServerComment"].Value.ToString() == website)
{
result = subDE.Children.Find("Root", "IIsWebVirtualDir");
break;
}
}
des = null;
return result;
}
private static int GetWebSiteInfo(int port)
{
int result = 1,i=1;
DirectoryEntries des = iisDE.Children;
foreach (DirectoryEntry subDE in des)
{
if (subDE.SchemaClassName == "IIsWebServer")
{
if ((i = Convert.ToInt32(subDE.Name)) >= result)
{
result = i + 1;
}
if (subDE.Properties["ServerBindings"][0].ToString() == ":" + port.ToString() + ":")
{
throw new Exception(" The port is already used");
}
}
}
des = null;
return result;
}
}
注意在很多文章中认为vde.Invoke("AppCreate", true);可有可无,但该方法能激活网站应用,相当于在一个虚拟目录中的Application settings中点击Create按钮来创建该目录的应用