/// <summary>
/// 建立虚拟网站 (适合IIS6)
/// </summary>
/// <param name="root">上阶目录,为空就是根目录</param>
/// <param name="path">实际路径</param>
/// <param name="name">站点名</param>
private void CreateWebsite(string root, string path, string name)
{
// Create virtual directory on the specified machineName
DirectoryEntry objDirEnt;
string temp = "";
if (root != "")
{
temp = "IIS://localhost/W3SVC/1/Root/" + root;
}
else
{
temp = "IIS://localhost/W3SVC/1/Root";
}
try
{
DirectoryEntry objDirPar = new DirectoryEntry(temp);
objDirEnt = objDirPar.Children.Add(name, "IIsWebVirtualDir");
objDirEnt.CommitChanges();
}
catch
{
objDirEnt = new DirectoryEntry(temp + "/" + name);
}
objDirEnt.Invoke("AppCreate", true);
objDirEnt.CommitChanges();
objDirEnt.Properties["AccessRead"][0] = true;
objDirEnt.Properties["AccessExecute"][0] = true;
objDirEnt.Properties["AccessScript"][0] = true;
objDirEnt.Properties["AuthFlags"][0] = 1;
//objDirEnt.Properties["DefaultDoc"][0] = "login.aspx";
//objDirEnt.Properties["EnableDefaultDoc"][0] = true;
objDirEnt.Properties["Path"][0] = path;
objDirEnt.CommitChanges();
}
// 以下适合IIS 7及以上版本
//增加IIS 网址
private void CreateApplication(string FullPath)
{
using (ServerManager iisManager = new ServerManager())
{
DeleteApplication();
iisManager.Sites["Default Web Site"].Applications.Add("/JSDevelop", FullPath).ApplicationPoolName = "Classic .NET AppPool";
iisManager.Sites["Default Web Site"].Applications.Add("/JSDevelop/Include", FullPath + "\\Include").ApplicationPoolName = "Classic .NET AppPool";
iisManager.CommitChanges();
}
}
private void DeleteApplication()
{
using (ServerManager iisManager = new ServerManager())
{
for (int i = 0; i < iisManager.Sites["Default Web Site"].Applications.Count; i++)
{
Microsoft.Web.Administration.Application App = iisManager.Sites["Default Web Site"].Applications[i];
if (App.Path.ToUpper().StartsWith("/JSDEVELOP"))
{
iisManager.Sites["Default Web Site"].Applications.Remove(App);
i--;
}
}
iisManager.CommitChanges();
}
}