亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

C#操作IIS程序池及站點的創建配置實現代碼

2020-01-24 03:30:33
字體:
來源:轉載
供稿:網友

首先要對Microsoft.Web.Administration進行引用,它主要是用來操作IIS7;

using System.DirectoryServices;
using Microsoft.Web.Administration;

1:首先是對本版IIS的版本進行配置:

復制代碼 代碼如下:

DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
            string Version = getEntity.Properties["MajorIISVersionNumber"].Value.ToString();
            MessageBox.Show("IIS版本為:" + Version);

2:是判斷程序池是存在;

復制代碼 代碼如下:

/// <summary>
        /// 判斷程序池是否存在
        /// </summary>
        /// <param name="AppPoolName">程序池名稱</param>
        /// <returns>true存在 false不存在</returns>
        private bool IsAppPoolName(string AppPoolName)
        {
            bool result = false;
            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            foreach (DirectoryEntry getdir in appPools.Children)
            {
                if (getdir.Name.Equals(AppPoolName))
                {
                    result = true;
                }
            }
            return result;
        }

3:刪除應用程序池

復制代碼 代碼如下:

/// <summary>
        /// 刪除指定程序池
        /// </summary>
        /// <param name="AppPoolName">程序池名稱</param>
        /// <returns>true刪除成功 false刪除失敗</returns>
        private bool DeleteAppPool(string AppPoolName)
        {
            bool result = false;
            DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
            foreach (DirectoryEntry getdir in appPools.Children)
            {
                if (getdir.Name.Equals(AppPoolName))
                {
                    try
                    {
                        getdir.DeleteTree();
                        result = true;
                    }
                    catch
                    {
                        result = false;
                    }
                }
            }
            return result;
        }

4:創建應用程序池 (對程序池的設置主要是針對IIS7;IIS7應用程序池托管模式主要包括集成跟經典模式,并進行NET版本的設置)

復制代碼 代碼如下:

string AppPoolName = "LamAppPool";
            if (!IsAppPoolName(AppPoolName))
            {
                DirectoryEntry newpool;
                DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
                newpool.CommitChanges();
                MessageBox.Show(AppPoolName + "程序池增加成功");
            }
            #endregion

            #region 修改應用程序的配置(包含托管模式及其NET運行版本)
            ServerManager sm = new ServerManager();
            sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
            sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經典
            sm.CommitChanges();
            MessageBox.Show(AppPoolName + "程序池托管管道模式:" + sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "運行的NET版本為:" + sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);

運用C#代碼來對IIS7程序池托管管道模式及版本進行修改;



5:針對IIS6的NET版進行設置;因為此處我是用到NET4.0所以V4.0.30319 若是NET2.0則在這進行修改 v2.0.50727

復制代碼 代碼如下:

//啟動aspnet_regiis.exe程序
            string fileName = Environment.GetEnvironmentVariable("windir") + @"/Microsoft.NET/Framework/v4.0.30319/aspnet_regiis.exe";
            ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
            //處理目錄路徑
            string path = vdEntry.Path.ToUpper();
            int index = path.IndexOf("W3SVC");
            path = path.Remove(0, index);
            //啟動ASPnet_iis.exe程序,刷新腳本映射
            startInfo.Arguments = "-s " + path;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            string errors = process.StandardError.ReadToEnd();

6:平常我們可能還得對IIS中的MIME類型進行增加;下面主要是我們用到兩個類型分別是:xaml,xap

復制代碼 代碼如下:

IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();
            NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";
            IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();
            TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";
            rootEntry.Properties["MimeMap"].Add(NewMime);
            rootEntry.Properties["MimeMap"].Add(TwoMime);
            rootEntry.CommitChanges();

7:下面是做安裝時一段對IIS進行操作的代碼;兼容IIS6及IIS7;新建虛擬目錄并對相應的屬性進行設置;對IIS7還進行新建程序池的程序;并設置程序池的配置;

復制代碼 代碼如下:

/// <summary>
    /// 創建網站
    /// </summary>
    /// <param name="siteInfo"></param>
      public  void CreateNewWebSite(NewWebSiteInfo siteInfo)
        {
            if (!EnsureNewSiteEnavaible(siteInfo.BindString))
            {
                throw new Exception("該網站已存在" + Environment.NewLine + siteInfo.BindString);
            }
            DirectoryEntry rootEntry = GetDirectoryEntry(entPath);

            newSiteNum = GetNewWebSiteID();
            DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");
            newSiteEntry.CommitChanges();

            newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;
            newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;
            newSiteEntry.CommitChanges();
            DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
            vdEntry.CommitChanges();
            string ChangWebPath = siteInfo.WebPath.Trim().Remove(siteInfo.WebPath.Trim().LastIndexOf('//'),1);
            vdEntry.Properties["Path"].Value = ChangWebPath;


            vdEntry.Invoke("AppCreate", true);//創建應用程序

            vdEntry.Properties["AccessRead"][0] = true; //設置讀取權限
            vdEntry.Properties["AccessWrite"][0] = true;
            vdEntry.Properties["AccessScript"][0] = true;//執行權限
            vdEntry.Properties["AccessExecute"][0] = false;
            vdEntry.Properties["DefaultDoc"][0] = "Login.aspx";//設置默認文檔
            vdEntry.Properties["AppFriendlyName"][0] = "LabManager"; //應用程序名稱          
            vdEntry.Properties["AuthFlags"][0] = 1;//0表示不允許匿名訪問,1表示就可以3為基本身份驗證,7為windows繼承身份驗證
            vdEntry.CommitChanges();

            //操作增加MIME
            //IISOle.MimeMapClass NewMime = new IISOle.MimeMapClass();
            //NewMime.Extension = ".xaml"; NewMime.MimeType = "application/xaml+xml";
            //IISOle.MimeMapClass TwoMime = new IISOle.MimeMapClass();
            //TwoMime.Extension = ".xap"; TwoMime.MimeType = "application/x-silverlight-app";
            //rootEntry.Properties["MimeMap"].Add(NewMime);
            //rootEntry.Properties["MimeMap"].Add(TwoMime);
            //rootEntry.CommitChanges();

            #region 針對IIS7
            DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
            int Version =int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());
            if (Version > 6)
            {
                #region 創建應用程序池
                string AppPoolName = "LabManager";
                if (!IsAppPoolName(AppPoolName))
                {
                    DirectoryEntry newpool;
                    DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
                    newpool = appPools.Children.Add(AppPoolName, "IIsApplicationPool");
                    newpool.CommitChanges();
                }
                #endregion

                #region 修改應用程序的配置(包含托管模式及其NET運行版本)
                ServerManager sm = new ServerManager();
                sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
                sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經典
                sm.CommitChanges();
                #endregion

                vdEntry.Properties["AppPoolId"].Value = AppPoolName;
                vdEntry.CommitChanges();
            }
            #endregion


            //啟動aspnet_regiis.exe程序
            string fileName = Environment.GetEnvironmentVariable("windir") + @"/Microsoft.NET/Framework/v4.0.30319/aspnet_regiis.exe";
            ProcessStartInfo startInfo = new ProcessStartInfo(fileName);
            //處理目錄路徑
            string path = vdEntry.Path.ToUpper();
            int index = path.IndexOf("W3SVC");
            path = path.Remove(0, index);
            //啟動ASPnet_iis.exe程序,刷新腳本映射
            startInfo.Arguments = "-s " + path;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();
            string errors = process.StandardError.ReadToEnd();
            if (errors != string.Empty)
            {
                throw new Exception(errors);
            }

        }

復制代碼 代碼如下:

string entPath = String.Format("IIS://{0}/w3svc", "localhost");

public  DirectoryEntry GetDirectoryEntry(string entPath)
       {
           DirectoryEntry ent = new DirectoryEntry(entPath);
           return ent;
       }

        public class NewWebSiteInfo
        {
            private string hostIP;   // 主機IP
            private string portNum;   // 網站端口號
            private string descOfWebSite; // 網站表示。一般為網站的網站名。例如"www.dns.com.cn"
            private string commentOfWebSite;// 網站注釋。一般也為網站的網站名。
            private string webPath;   // 網站的主目錄。例如"e:/ mp"

            public NewWebSiteInfo(string hostIP, string portNum, string descOfWebSite, string commentOfWebSite, string webPath)
            {
                this.hostIP = hostIP;
                this.portNum = portNum;
                this.descOfWebSite = descOfWebSite;
                this.commentOfWebSite = commentOfWebSite;
                this.webPath = webPath;
            }

            public string BindString
            {
                get
                {
                    return String.Format("{0}:{1}:{2}", hostIP, portNum, descOfWebSite); //網站標識(IP,端口,主機頭值)
                }
            }

            public string PortNum
            {
                get
                {
                    return portNum;
                }
            }

            public string CommentOfWebSite
            {
                get
                {
                    return commentOfWebSite;
                }
            }

            public string WebPath
            {
                get
                {
                    return webPath;
                }
            }
        }

8:下面的代碼是對文件夾權限進行設置,下面代碼是創建Everyone 并給予全部權限

復制代碼 代碼如下:

/// <summary>
        /// 設置文件夾權限 處理給EVERONE賦予所有權限
        /// </summary>
        /// <param name="FileAdd">文件夾路徑</param>
        public void SetFileRole()
        {
            string FileAdd = this.Context.Parameters["installdir"].ToString();
            FileAdd = FileAdd.Remove(FileAdd.LastIndexOf('//'), 1);
            DirectorySecurity fSec = new DirectorySecurity();
            fSec.AddAccessRule(new FileSystemAccessRule("Everyone",FileSystemRights.FullControl,InheritanceFlags.ContainerInherit|InheritanceFlags.ObjectInherit,PropagationFlags.None,AccessControlType.Allow));
            System.IO.Directory.SetAccessControl(FileAdd, fSec);
        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
成人www视频在线观看| 国产精品永久免费观看| xxxx欧美18另类的高清| 亚洲精品国精品久久99热一| 欧美日本中文字幕| 欧美亚洲视频在线观看| 欧日韩不卡在线视频| 亚洲精品第一页| 国内免费久久久久久久久久久| 欧美激情小视频| 亚洲第一精品夜夜躁人人爽| 成人精品一区二区三区电影黑人| 日韩av色在线| 国产精品88a∨| 自拍视频国产精品| 日韩国产精品亚洲а∨天堂免| 91在线观看免费网站| 成人免费高清完整版在线观看| 亚洲成人999| 亚洲深夜福利视频| 国产成人亚洲综合青青| 欧洲亚洲在线视频| 777国产偷窥盗摄精品视频| 国产精品亚洲一区二区三区| 成人久久一区二区三区| 日韩在线观看免费| 奇门遁甲1982国语版免费观看高清| 亚洲网站视频福利| 日韩免费在线电影| 亚洲成人网久久久| 国产精品三级美女白浆呻吟| 日本精品性网站在线观看| 欧美成人免费全部| 久久精品国产2020观看福利| 77777少妇光屁股久久一区| 亚洲欧美999| 欧美激情中文网| 欧美精品一区三区| 日韩成人黄色av| 最近2019中文字幕大全第二页| 91免费精品国偷自产在线| 精品美女国产在线| 成人免费视频在线观看超级碰| 国产精品永久免费视频| 久久99国产精品自在自在app| 国产精品成人av在线| 国产有码在线一区二区视频| 欧美日本精品在线| 久久亚洲电影天堂| 欧美精品免费在线观看| 亚洲电影中文字幕| 久久精品人人做人人爽| 在线视频欧美性高潮| 欧美日韩性视频| 亚洲精品乱码久久久久久金桔影视| 国产精品精品视频| 亚洲男人的天堂网站| 97国产精品视频人人做人人爱| 国产成人综合一区二区三区| 欧美高清在线播放| 国产精品久久久久一区二区| 一区二区欧美久久| 影音先锋欧美在线资源| 欧美福利视频网站| 亚洲成人国产精品| 国产精品久久久久久久久久免费| 欧美色播在线播放| 国产日韩在线看片| 久久国产天堂福利天堂| 亚洲精品丝袜日韩| 欧美在线激情网| 国产精品va在线播放我和闺蜜| 亚洲精品乱码久久久久久按摩观| 日本欧美中文字幕| 国产香蕉一区二区三区在线视频| 久久免费福利视频| 欧美日韩一区二区免费在线观看| 91tv亚洲精品香蕉国产一区7ujn| 亚洲最大激情中文字幕| 亚洲精品日韩激情在线电影| 538国产精品视频一区二区| 欧美激情按摩在线| 国产激情999| 激情亚洲一区二区三区四区| 国产美女精品视频免费观看| 永久免费看mv网站入口亚洲| 97在线免费观看视频| 欧美中文在线字幕| 2025国产精品视频| 性欧美亚洲xxxx乳在线观看| 国产日韩欧美在线视频观看| 亚洲欧美在线一区二区| 中文字幕成人在线| 久久久久久久91| 国内精品小视频在线观看| 精品免费在线视频| 奇米4444一区二区三区| 日韩av电影手机在线观看| 国产精品入口免费视频一| 久久久久免费视频| 亚洲人成免费电影| 亚洲男人第一网站| 日韩精品中文字幕在线播放| 91国自产精品中文字幕亚洲| 亚洲嫩模很污视频| 日韩高清电影免费观看完整版| 色悠悠久久88| 久久久久一本一区二区青青蜜月| 国产精自产拍久久久久久| 国产999精品久久久| 国产日韩中文在线| 丁香五六月婷婷久久激情| 欧美中文字幕在线视频| 日韩电视剧在线观看免费网站| 欧美大胆在线视频| 日韩大片在线观看视频| 国产精品草莓在线免费观看| 欧美日韩亚洲91| 国产成人精品一区| 欧美人成在线视频| 欧美在线观看视频| 欧美中文字幕视频在线观看| 日韩精品久久久久久久玫瑰园| 欧美丰满老妇厨房牲生活| 亚洲日本aⅴ片在线观看香蕉| 欧亚精品中文字幕| 在线观看国产成人av片| 在线播放日韩av| 精品丝袜一区二区三区| 在线日韩第一页| 国产精品成人aaaaa网站| 亚洲3p在线观看| 亚洲最新在线视频| 国产亚洲视频中文字幕视频| 久久久久久久久亚洲| 亚洲精品综合久久中文字幕| 精品中文字幕在线观看| 国产一区二区美女视频| 成人午夜激情免费视频| 亚洲专区国产精品| 日韩毛片在线看| 欧美夫妻性视频| 日韩一二三在线视频播| 精品日本美女福利在线观看| 日本精品中文字幕| 日韩电影在线观看免费| 国产精品中文久久久久久久| 亚洲视频在线免费看| 最近中文字幕日韩精品| 日韩欧美国产激情| 日av在线播放中文不卡| 97久久精品人搡人人玩| 欧美视频裸体精品| 国产精品尤物福利片在线观看| 性欧美办公室18xxxxhd| 久久韩国免费视频| 国产精品视频免费在线观看| 久久久999精品视频| 亚洲va欧美va国产综合剧情| 久久久亚洲欧洲日产国码aⅴ| 亚洲精品福利在线| 91成人免费观看网站| 国产日本欧美一区二区三区在线| 97视频免费观看|