1. 程式人生 > >C#實現動態釋出IIS站點幫助類

C#實現動態釋出IIS站點幫助類

準備工作:

1、引用 System.DirectoryServices 系統程式集

2、引用 Microsoft.Web.Administration 程式集,類庫位置在 C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll ,直接拷貝到專案引用即可

3、呼叫方式:

                string bing = string.Format("{0}:{1}:", item.BingIp, item.Port);
                bool
result = IISManager.CreateWebSite(item.SiteName, filePath, bing);

4、原始碼:

public class IISManager
    {
        /// <summary>
        /// 建立一個站點
        /// </summary>
        /// <param name="name">站點名稱</param>
        /// <param name="physicalPath">專案所在路徑</param>
/// <param name="bindingInformation">繫結資訊</param> /// <param name="bindingProtocol">型別,預設http</param> /// <returns></returns> public static bool CreateWebSite(string name, string physicalPath, string bindingInformation = "*:80:", string bindingProtocol = "
http") { try { ServerManager manager = new ServerManager(); //判斷應用程式池是否存在 if (manager.ApplicationPools[name] != null) { manager.ApplicationPools.Remove(manager.ApplicationPools[name]); } //判斷web應用程式是否存在 if (manager.Sites[name] != null) { manager.Sites.Remove(manager.Sites[name]); } manager.Sites.Add(name, bindingProtocol, bindingInformation, physicalPath); //新增web應用程式池 ApplicationPool pool = manager.ApplicationPools.Add(name); //設定web應用程式池的Framework版本 pool.ManagedRuntimeVersion = "v4.0"; //設定是否啟用32為應用程式 pool.SetAttributeValue("enable32BitAppOnWin64", true); //設定web網站的應用程式池 manager.Sites[name].Applications[0].ApplicationPoolName = name; manager.CommitChanges(); return true; } catch (Exception e) { return false; } } /// <summary> /// 建立一個站點 /// </summary> /// <param name="name">站點名稱</param> /// <param name="physicalPath">專案所在路徑</param> /// <param name="port">埠號</param> /// <returns></returns> public static bool CreateWebSite(string name, string physicalPath, int port = 80) { try { ServerManager manager = new ServerManager(); //判斷應用程式池是否存在 if (manager.ApplicationPools[name] != null) { manager.ApplicationPools.Remove(manager.ApplicationPools[name]); } //判斷web應用程式是否存在 if (manager.Sites[name] != null) { manager.Sites.Remove(manager.Sites[name]); } manager.Sites.Add(name, physicalPath, port); //新增web應用程式池 ApplicationPool pool = manager.ApplicationPools.Add(name); //設定web應用程式池的Framework版本 pool.ManagedRuntimeVersion = "v4.0"; //設定是否啟用32為應用程式 pool.SetAttributeValue("enable32BitAppOnWin64", true); //設定web網站的應用程式池 manager.Sites[name].Applications[0].ApplicationPoolName = name; manager.CommitChanges(); return true; } catch (Exception e) { return false; } } /// <summary> /// 建立虛擬目錄 /// </summary> /// <param name="vDirName">虛擬目錄名稱</param> /// <param name="path">實際路徑</param> /// <param name="iAuth">設定目錄的安全性 0-不允許匿名訪問,1-為允許,2-基本身份驗證,3-允許匿名+基本身份驗證,4-整合Windows驗證,5-允許匿名+整合Windows驗證</param> /// <param name="serverName">預設localhost</param> /// <returns></returns> public static bool CreateVirtualDirectory(string vDirName, string path, int iAuth = 1, string serverName = "localhost") { try { // 確定IIS版本   DirectoryEntry iisSchema = new DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated"); bool iisUnderNt = iisSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN"; iisSchema.Dispose(); // 獲得管理許可權 DirectoryEntry iisAdmin = new DirectoryEntry("IIS://" + serverName + "/W3SVC/1/Root"); // 如果虛擬目錄已經存在則刪除   foreach (DirectoryEntry v in iisAdmin.Children) { if (v.Name == vDirName) { try { iisAdmin.Invoke("Delete", new object[] { v.SchemaClassName, vDirName }); iisAdmin.CommitChanges(); } catch (Exception ex) { return false; } } } // 建立一個虛擬目錄 DirectoryEntry vDir = iisAdmin.Children.Add(vDirName, "DefaultWebSiteVirtualDir"); // 建立一個web應用 vDir.Invoke("AppCreate", !iisUnderNt); //應用程式名稱                 vDir.Properties["AppFriendlyName"][0] = vDirName; //設定讀取許可權                 vDir.Properties["AccessRead"][0] = true; //值 true 表示不論檔案型別是什麼,檔案或資料夾的內容都可以執行                 vDir.Properties["AccessExecute"][0] = false; //值 true 表示允許使用者將檔案及其相關屬性上載到伺服器上已啟用的目錄中,或者更改可寫檔案的內容。 //只有使用支援 HTTP 1.1 協議標準的 PUT 功能的瀏覽器,才能執行寫入操作 vDir.Properties["AccessWrite"][0] = false; //值 true 表示如果是指令碼檔案或靜態內容,則可以執行檔案或資料夾的內容。值 false 只允許提供靜態檔案,如 HTML 檔案 vDir.Properties["AccessScript"][0] = true; //設定為 true 時,瀏覽目錄時系統會載入該目錄的預設文件(由 De, faultDoc 屬性指定)                 vDir.Properties["EnableDefaultDoc"][0] = true; //設定為 true 時,將啟用目錄瀏覽 vDir.Properties["EnableDirBrowsing"][0] = false; //包含一個或多個預設文件的檔名,如果在客戶端的請求中不包含檔名,將把預設文件的檔名返回給客戶端 vDir.Properties["DefaultDoc"][0] = "login.html,index.html,default.html,Default.aspx,index.aspx"; //專案路徑                 vDir.Properties["Path"][0] = path; //作為有效方案返回給客戶端的 Windows 驗證方案的設定 vDir.Properties["AuthFlags"][0] = iAuth; // NT格式不支援這特性 if (!iisUnderNt) { //頁面是否允許當前目錄的相對路徑(使用 ..\ 表示法) vDir.Properties["AspEnableParentPaths"][0] = true; } // 設定改變 vDir.CommitChanges(); return true; } catch (Exception ex) { return false; } } }