首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 操作 IIS 7.0 完整指南:站點、虛擬目錄、應用程式集區管理

     編輯:關於C#

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;
}

檢查 Bindings 資訊是否存在

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;
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved