DeleteSite
/// <summary>
/// Delete an existent web site.
/// </summary>
/// <param name="siteName">Site name.</param>
public static void DeleteSite(string siteName)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
mgr.Sites.Remove(site);
mgr.CommitChanges();
}
}
} 然后是对虚拟目录的操作,包括创建和删除虚拟目录,都比较简单。
CreateVDir
public static void CreateVDir(string siteName, string vDirName, string physicalPath)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site == null)
{
throw new ApplicationException(String.Format("Web site {0} does not exist", siteName));
}
site.Applications.Add("/" + vDirName, physicalPath);
mgr.CommitChanges();
}
}
DeleteVDir
public static void DeleteVDir(string siteName, string vDirName)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
Microsoft.Web.Administration.Application app = site.Applications["/" + vDirName];
if (app != null)
{
site.Applications.Remove(app);
mgr.CommitChanges();
}
}
}
} 删除应用程序池。
DeletePool
/// <summary>
/// Delete an existent web site app pool.
/// </summary>
/// <param name="appPoolName">App pool name for deletion.</param>
public static void DeletePool(string appPoolName)
{
using (ServerManager mgr = new ServerManager())
{
ApplicationPool pool = mgr.ApplicationPools[appPoolName];
if (pool != null)
{
mgr.ApplicationPools.Remove(pool);
mgr.CommitChanges();
}
}
} 在站点上添加默认文档。
foreach (ConfigurationElement elt in filesCollection)
{
if (elt.Attributes["value"].Value.ToString() == defaultDocName)
{
return;
}
}
try
{
ConfigurationElement docElement = filesCollection.CreateElement();
docElement.SetAttributeValue("value", defaultDocName);
filesCollection.Add(docElement);
}
catch (Exception) { } //this will fail if existing
mgr.CommitChanges();
}
} 检查虚拟目录是否存在。
VerifyVirtualPathIsExist
public static bool VerifyVirtualPathIsExist(string siteName, string path)
{
using (ServerManager mgr = new ServerManager())
{
Site site = mgr.Sites[siteName];
if (site != null)
{
foreach (Microsoft.Web.Administration.Application app in site.Applications)
{
if (app.Path.ToUpper().Equals(path.ToUpper()))
{
return true;
}
}
}
}
return false;
} 检查站点是否存在。
VerifyWebSiteIsExist
public static bool VerifyWebSiteIsExist(string siteName)
{
using (ServerManager mgr = new ServerManager())
{
for (int i = 0; i < mgr.Sites.Count; i++)
{
if (mgr.Sites.Name.ToUpper().Equals(siteName.ToUpper()))
{
return true;
}
}
}
return false;
} 检查Bindings信息。
VerifyWebSiteBindingsIsExist
public static bool VerifyWebSiteBindingsIsExist(string bindingInfo)
{
string temp = string.Empty;
using (ServerManager mgr = new ServerManager())
{
for (int i = 0; i < mgr.Sites.Count; i++)
{
foreach (Microsoft.Web.Administration.Binding b in mgr.Sites.Bindings)
{
temp = b.BindingInformation;
if (temp.IndexOf('*') < 0)
{
temp = "*" + temp;
}
if (temp.Equals(bindingInfo))
{
return true;
}
}
}
}
return false;
} 以上代码均在Windows Vista SP1和Windows Server 2008上测试通过,使用时需要在工程中引用Microsoft.Web.Administration类库,该类库为IIS 7.0自带的。