C#實現動態發佈IIS站點幫助類

準備工做:html

一、引用 System.DirectoryServices 系統程序集web

二、引用 Microsoft.Web.Administration 程序集,類庫位置在 C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll ,直接拷貝到項目引用便可瀏覽器

三、調用方式:安全

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

四、源碼:服務器

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

    }
相關文章
相關標籤/搜索