Microsoft 隨 Windows Vista 發布 IIS 7.0,提供許多新特性:完全模組化元件、文字檔配置功能、MMC 圖形化管理工具,並與 .NET 程式語言結合更緊密。新增的 Microsoft.Web.Administration 命名空間增加許多管理與存取 IIS 的物件,使透過程式設計操作 IIS 更簡便。雖然 IIS 6.0 時代也能輕鬆用 C# 管理 IIS,但現在所需程式碼更少、功能更強。以下是我專案中寫的一個類別庫片段,主要實現對 IIS 7.0 操作:建立/刪除站點、虛擬目錄、應用程式集區、新增預設文件、判斷站點與虛擬目錄是否存在、檢查 Bindings 資訊等。
對 IIS 7.0 介紹有興趣可參考:
使用時需在專案引用 Microsoft.Web.Administration 組件(IIS 7.0 自帶)。以下程式碼均在 Windows Vista SP1 與 Windows Server 2008 測試通過。
///
/// 建立新網站
///
/// 站點名稱
/// 繫結資訊,格式 "*:<port>:<hostname>",例 "*:80:myhost.com"
/// 實體路徑
public static void CreateSite(string siteName, string bindingInfo, string physicalPath) {
createSite(siteName, "http", bindingInfo, physicalPath, true, siteName + "Pool",
ProcessModelIdentityType.NetworkService, null, null, ManagedPipelineMode.Integrated, null);
}
private static void createSite(string siteName, string protocol, string bindingInformation, string physicalPath,
bool createAppPool, string appPoolName, ProcessModelIdentityType identityType,
string appPoolUserName, string appPoolPassword, ManagedPipelineMode appPoolPipelineMode, string managedRuntimeVersion) {
using (ServerManager mgr = new ServerManager()) {
Site site = mgr.Sites.Add(siteName, protocol, bindingInformation, physicalPath);
// 依需求建立應用程式集區
if (createAppPool) {
ApplicationPool pool = mgr.ApplicationPools.Add(appPoolName);
if (pool.ProcessModel.IdentityType != identityType)
pool.ProcessModel.IdentityType = identityType;
if (!String.IsNullOrEmpty(appPoolUserName)) {
pool.ProcessModel.UserName = appPoolUserName;
pool.ProcessModel.Password = appPoolPassword;
}
if (appPoolPipelineMode != pool.ManagedPipelineMode)
pool.ManagedPipelineMode = appPoolPipelineMode;
site.Applications["/"].ApplicationPoolName = pool.Name;
}
mgr.CommitChanges();
}
}
///
/// 刪除現有網站
///
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();
}
}
}
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();
}
}
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();
}
}
}
}
///
/// 刪除現有網站應用程式集區
///
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();
}
}
}
public static void AddDefaultDocument(string siteName, string defaultDocName) {
using (ServerManager mgr = new ServerManager()) {
Configuration cfg = mgr.GetWebConfiguration(siteName);
ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
ConfigurationElementCollection filesCollection = filesElement.GetCollection();
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) { } // 若已存在會失敗
mgr.CommitChanges();
}
}
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;
}
public static bool VerifyWebSiteIsExist(string siteName) {
using (ServerManager mgr = new ServerManager()) {
for (int i = 0; i < mgr.Sites.Count; i++) {
if (mgr.Sites[i].Name.ToUpper().Equals(siteName.ToUpper()))
return true;
}
}
return false;
}
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[i].Bindings) {
temp = b.BindingInformation;
if (temp.IndexOf('*') < 0)
temp = "*" + temp;
if (temp.Equals(bindingInfo))
return true;
}
}
}
return false;
}