用程序的方法在IIS建立站点或虚拟目录(C#)
目的:实现自动建立站点和虚拟目录
平台:windows, vs.net
public class IISHelper
{
static DirectoryEntry iisDE = new DirectoryEntry("IIS://localhost/W3SVC");
///<summary>
/// Get The Location IIS WebServers Information
///</summary>
///<returns></returns>
public static Hashtable GetLocationIIsWebServers()
{
Hashtable result = new Hashtable();
GetWebSiteInfo(ref result);
return result;
}
///<summary>
/// Create a new IIS Webserver
///</summary>
///<param name="webServerName">webserver name</param>
///<param name="path">webserver directory path</param>
///<param name="port">access port</param>
public static void CreateNewIIsWebServer(string webServerName,string path,int port)
{
int siteID=GetWebSiteInfo(port);
using (DirectoryEntry site = (DirectoryEntry)iisDE.Invoke("Create", "IIsWebServer", siteID))
{
site.Invoke("Put", "ServerComment", webServerName);
site.Invoke("Put", "KeyType", "IIsWebServer");
site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");
site.Invoke("Put", "ServerState", 2);
site.Invoke("Put", "FrontPageWeb", 1);
site.Invoke("Put", "DefaultDoc", "index.aspx");
site.Invoke("Put", "SecureBindings", ":443:");
site.Invoke("Put", "ServerAutoStart", 1);
site.Invoke("Put", "ServerSize", 1);
site.Invoke("SetInfo");
using (DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir"))
{
siteVDir.Properties["AppIsolated"][0] = 2;
siteVDir.Properties["Path"][0] = path;
siteVDir.Properties["AccessFlags"][0] = 513;
siteVDir.Properties["FrontPageWeb"][0] = 1;
//siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/"+webServerName;
siteVDir.Properties["AppFriendlyName"][0] = webServerName;
siteVDir.Invoke("AppCreate", true);
siteVDir.CommitChanges();
}
site.CommitChanges();
}
}
///<summary>
/// Create a new virtual directory
///</summary>
///<param name="website">webserver name</param>
///<param name="dirName">virtual directory name</param>
///<param name="properties">virtual dirctory properties</param>
public static void CreateNewVirtualDir(string website, string dirName, System.Data.PropertyCollection properties)
{
if(GetVirtualDir(website, dirName)) throw new Exception(" The Virtual Dir is alread existed");
using (DirectoryEntry de = GetWebSiteInfo(website))
{
using (DirectoryEntry vde = de.Children.Add(dirName, "IIsWebVirtualDir"))
{
vde.CommitChanges();
de.CommitChanges();
UpdateVDirInfo(vde, ref properties);
vde.Invoke("AppCreate", true);
vde.CommitChanges();
de.CommitChanges();
}
}
}
///<summary>
/// Get Common virtual directory setting
///</summary>
///<param name="path">virtual directory path</param>
///<param name="vdir">virtual directory name</param>
///<returns></returns>
public static System.Data.PropertyCollection GetDefaultVirtualDirSetting(string path,string vdir)
{
System.Data.PropertyCollection vProperty = new System.Data.PropertyCollection();
//vProperty.Add("AnonymousUserName","");
//vProperty.Add("AnonymousUserPass","");
vProperty.Add("AccessRead", true);
vProperty.Add("AccessExecute", false);
vProperty.Add("AuthBasic", true);
vProperty.Add("AuthNTLM", true);
vProperty.Add("ContentIndexed", true);
vProperty.Add("EnableDefaultDoc", true);
vProperty.Add("EnableDirBrowsing", false);
vProperty.Add("AccessSSL", false);
vProperty.Add("AccessScript", false);
vProperty.Add("DefaultDoc", "index.aspx");
vProperty.Add("Path", path);
vProperty.Add("AppIsolated", 2);
vProperty.Add("AppFriendlyName", vdir);
vProperty.Add("AccessFlags", 513);
vProperty.Add("FrontPageWeb", 1);
//vProperty.Add("DontLog", true);
//vProperty.Add("AppRoot", "LM/W3SVC/" + siteID + "/" + vdir);
return vProperty;
}
|