如何管理IIS,自动创建WEB SITE,应用程序池?

有时我们需要动态的创建WEB SITE或都是虚拟目录,比如,当用户申请一个社区后,分配一个二级域名的,一个用户一个Web site。这时我们就可以用到。NET访问IIS的功能,来自动创建了。

需要引用using System.DirectoryServices;这个组件来管理IIS,如果是WEB来访问的话还有权限的问题,一般这样并发的要求性能较高的操作都写成SERVICE后台来处理比较合适。

请看下面的代码片段,

/// <summary>

/// 创建应用程序池

/// </summary>

/// <param name="AppPoolName"></param>

public static void CreateAppPool(string AppPoolName)

{

bool ExistAppPoolFlag = false;

DirectoryEntry newpool;

DirectoryEntry apppools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

foreach (DirectoryEntry a in apppools.Children)

{

if (a.Name == AppPoolName)

{

ExistAppPoolFlag = true;

break;

}

}

if (ExistAppPoolFlag == false)

{

newpool = apppools.Children.Add(AppPoolName, "IIsApplicationPool");

newpool.CommitChanges();

}

}

/// <summary>

/// 给某一个site分配程序池

/// </summary>

/// <param name="newvdir">The newvdir.</param>

/// <param name="AppPoolName">Name of the app pool.</param>

public static void AssignAppPool(DirectoryEntry newvdir, string AppPoolName)

{

newvdir.Properties["AppPoolId"].Value = AppPoolName;

newvdir.CommitChanges();

}

//下面这个是修改Web Site的执行脚本。 通常建一个web site default的asp.net 1.1

/// <summary>

/// Updates the aspdotnet script version.

/// </summary>

/// <param name="UpdateScriptPath">The update script path.</param>

public static void UpdateAspdotnetScriptVersion(DirectoryEntry siteVDir,string UpdateScriptPath)

{

string fileName = UpdateScriptPath;

ProcessStartInfo startInfo = new ProcessStartInfo(fileName);

if (startInfo != null)

{

//处理目录路径

string path = siteVDir.Path.ToUpper();

int index = path.IndexOf("W3SVC");

path = path.Remove(0, index);

//启动aspnet_iis.exe程序,刷新教本映射

startInfo.Arguments = "-s " + path;

startInfo.WindowStyle = ProcessWindowStyle.Hidden;

startInfo.UseShellExecute = false;

startInfo.CreateNoWindow = true;

startInfo.RedirectStandardOutput = true;

startInfo.RedirectStandardError = true;

Process process = new Process();

process.StartInfo = startInfo;

process.Start();

process.WaitForExit();

string errors = process.StandardError.ReadToEnd();

if (errors != string.Empty)

{

throw new Exception(errors);

}

process.Dispose();

}

}

//创建一个web site的虚似目录

/// <summary>

/// Createirtuals the directory.

/// </summary>

/// <param name="nameDirectory">The name directory.</param>

/// <param name="VirDirSchemaName">Name of the vir dir schema.</param>

public static void CreateirtualDirectory(string virtualDirName, string physicalPath, int webSiteID)

{

String constIISWebSiteRoot = "IIS://localhost/W3SVC/"+webSiteID+"/ROOT";

DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);

DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName);

tbEntry.Properties["Path"][0] = physicalPath;//设置虚拟目录指向的物理路径

tbEntry.Invoke("AppCreate2", false);//是否创建应用程序池

//AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path

tbEntry.Properties["AccessRead"][0] = true;////设置读取权限

tbEntry.Properties["AccessWrite"][0] = true;

tbEntry.Properties["AccessExecute"][0] = false;

tbEntry.Properties["AccessScript"][0] = false;

//tbEntry.Properties["ContentIndexed"][0] = true;

tbEntry.Properties["AppIsolated"].Value = 2;

tbEntry.Properties["EnableDefaultDoc"][0] = false;

tbEntry.Properties["UNCPassword"][0] = "aa";

tbEntry.Properties["UNCUserName"][0] = "bb";

//设置访问的default document

//tbEntry.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";

//tbEntry.Properties["AppFriendlyName"][0] = virtualDirName;//应用程序名称

//tbEntry.Properties["AccessScript"][0] = false;//执行权限

tbEntry.Properties["DontLog"][0] = true;

//设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证

tbEntry.Properties["AuthFlags"][0] = 3;

tbEntry.CommitChanges();

root.CommitChanges();

}

//创建一个新的Web site

/// <summary>

/// Creates a web site

/// </summary>

/// <param name="name">The name of the site</param>

/// <param name="homeDirectory">The site's home direcotry</param>

/// <param name="port">The web site's port</param>

/// <returns>The newly created site</returns>

public static IIsWebSite CreateWebSite(string name, string homeDirectory, int port,string CreateNewPool)

{

if (name == null || name.Length == 0)

{

throw new ArgumentException("You must specify a web site name");

}

if (homeDirectory == null || homeDirectory.Length == 0)

{

throw new ArgumentException("You must specify a home directory for the web site");

}

if (port < 0)

{

throw new ArgumentException("Your specified port must be greater then or equal to 0");

}

DirectoryEntry root = new DirectoryEntry(IIsConstants.IIsW3SvcPath);

// Finds an open site ID (in IIs 5 it is sequential - in IIs 6 they are random) to try and make it

// work for both, we will just find the largest current one and add one to it

int siteID = 1;

foreach(DirectoryEntry e in root.Children)

{

if(e.SchemaClassName == IIsConstants.IIsWebServerName)

{

int ID = Convert.ToInt32(e.Name);

if(ID > siteID)

{

siteID = ID;

}

}

}

siteID += 1;

// Create the site

DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", IIsConstants.IIsWebServerName, siteID);

site.Invoke("Put", "ServerComment", name);

site.Invoke("Put", "KeyType", IIsConstants.IIsWebServerName);

//site.Invoke("Put", "ServerBindings", ":" + port.ToString() + ":");

site.Invoke("Put", "ServerState", 1);

site.Invoke("Put", "FrontPageWeb", 1);

site.Invoke("Put", "DefaultDoc", "default.aspx");

//site.Invoke("Put", "SecureBindings", ":443:");//设置SSL

site.Invoke("Put", "ServerAutoStart", 0);

site.Invoke("Put", "ServerSize", 1);

site.Invoke("Put", "AccessRead", true);

site.Invoke("Put", "AccessScript", true);

site.Invoke("Put", "AccessWrite", false);

site.Invoke("Put", "EnableDirBrowsing", false);

site.Invoke("Put", "AppFriendlyName", name);

site.Invoke("SetInfo");

// Create the application virtual directory

DirectoryEntry siteVDir = site.Children.Add("Root", "IIsWebVirtualDir");

siteVDir.Properties["AppIsolated"][0] = 2;

siteVDir.Properties["Path"][0] = homeDirectory;

siteVDir.Properties["AccessFlags"][0] = 513;

siteVDir.Properties["FrontPageWeb"][0] = 1;

siteVDir.Properties["AppRoot"][0] = "/LM/W3SVC/" + siteID + "/Root";

siteVDir.CommitChanges();

//site.CommitChanges();

AddHostHeader(siteID, null, port, name);

if (CreateNewPool.Length > 0)

{

CreateAppPool(CreateNewPool);

AssignAppPool(site, CreateNewPool);

}

string Path = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe ";

UpdateAspdotnetScriptVersion(site,Path);

CreateirtualDirectory("album",@"\\192.168.1.10\FileUpLoad",siteID);

// retrieve the entry for the site

DirectoryEntry entry = new DirectoryEntry(IIsConstants.IIsW3SvcPath + "/" + siteID);

// to make the site show up correctly we need to stop and start it, but we also

// want to save the current started site

IIsWebSite newWebSite = IIsWebSite.FromDirectoryEntry(entry);

IIsWebSite currentWebSite = IIsAdministrator.GetWebSites().ActiveWebSite;

if(currentWebSite != null)

{

//currentWebSite.Stop();

newWebSite.Start();

//newWebSite.Stop();

currentWebSite.Start();

}

else

{

newWebSite.Start();

newWebSite.Stop();

}

// return a new wrapper around the site

return newWebSite;

}

问题:我可以正确的创建虚拟目录并指向一台远程的主机,把访问的username & password设置,但它始终是一个web application ,我不想让他成为一个application,只是一个文件夹虚拟目录用来指向远程主机。这个不知道用代码如何现实在

CreateirtualDirectory这个方法里,试过N次不行,有知道的请留言给我。谢谢

,/author]Leung[author]