欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#操作IIS程序池及站點(diǎn)的創(chuàng)建配置實(shí)現(xiàn)代碼

 更新時(shí)間:2013年03月29日 16:24:32   作者:  
最近在做一個(gè)WEB程序的安裝包;對(duì)一些操作IIS進(jìn)行一個(gè)簡單的總結(jié);主要包括對(duì)IIS進(jìn)行站點(diǎn)的新建以及新建站點(diǎn)的NET版本的選擇,還有針對(duì)IIS7程序池的托管模式以及版本的操作

首先要對(duì)Microsoft.Web.Administration進(jìn)行引用,它主要是用來操作IIS7;

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

1:首先是對(duì)本版IIS的版本進(jìn)行配置:

復(fù)制代碼 代碼如下:

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

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

復(fù)制代碼 代碼如下:

/// <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:刪除應(yīng)用程序池

復(fù)制代碼 代碼如下:

/// <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:創(chuàng)建應(yīng)用程序池 (對(duì)程序池的設(shè)置主要是針對(duì)IIS7;IIS7應(yīng)用程序池托管模式主要包括集成跟經(jīng)典模式,并進(jìn)行NET版本的設(shè)置)

復(fù)制代碼 代碼如下:

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 修改應(yīng)用程序的配置(包含托管模式及其NET運(yùn)行版本)
            ServerManager sm = new ServerManager();
            sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
            sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經(jīng)典
            sm.CommitChanges();
            MessageBox.Show(AppPoolName + "程序池托管管道模式:" + sm.ApplicationPools[AppPoolName].ManagedPipelineMode.ToString() + "運(yùn)行的NET版本為:" + sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion);

運(yùn)用C#代碼來對(duì)IIS7程序池托管管道模式及版本進(jìn)行修改;



5:針對(duì)IIS6的NET版進(jìn)行設(shè)置;因?yàn)榇颂幬沂怯玫絅ET4.0所以V4.0.30319 若是NET2.0則在這進(jìn)行修改 v2.0.50727

復(fù)制代碼 代碼如下:

//啟動(dòng)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);
            //啟動(dòng)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:平常我們可能還得對(duì)IIS中的MIME類型進(jìn)行增加;下面主要是我們用到兩個(gè)類型分別是:xaml,xap

復(fù)制代碼 代碼如下:

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:下面是做安裝時(shí)一段對(duì)IIS進(jìn)行操作的代碼;兼容IIS6及IIS7;新建虛擬目錄并對(duì)相應(yīng)的屬性進(jìn)行設(shè)置;對(duì)IIS7還進(jìn)行新建程序池的程序;并設(shè)置程序池的配置;

復(fù)制代碼 代碼如下:

/// <summary>
    /// 創(chuàng)建網(wǎng)站
    /// </summary>
    /// <param name="siteInfo"></param>
      public  void CreateNewWebSite(NewWebSiteInfo siteInfo)
        {
            if (!EnsureNewSiteEnavaible(siteInfo.BindString))
            {
                throw new Exception("該網(wǎng)站已存在" + 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);//創(chuàng)建應(yīng)用程序

            vdEntry.Properties["AccessRead"][0] = true; //設(shè)置讀取權(quán)限
            vdEntry.Properties["AccessWrite"][0] = true;
            vdEntry.Properties["AccessScript"][0] = true;//執(zhí)行權(quán)限
            vdEntry.Properties["AccessExecute"][0] = false;
            vdEntry.Properties["DefaultDoc"][0] = "Login.aspx";//設(shè)置默認(rèn)文檔
            vdEntry.Properties["AppFriendlyName"][0] = "LabManager"; //應(yīng)用程序名稱          
            vdEntry.Properties["AuthFlags"][0] = 1;//0表示不允許匿名訪問,1表示就可以3為基本身份驗(yàn)證,7為windows繼承身份驗(yàn)證
            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 針對(duì)IIS7
            DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO");
            int Version =int.Parse(getEntity.Properties["MajorIISVersionNumber"].Value.ToString());
            if (Version > 6)
            {
                #region 創(chuàng)建應(yīng)用程序池
                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 修改應(yīng)用程序的配置(包含托管模式及其NET運(yùn)行版本)
                ServerManager sm = new ServerManager();
                sm.ApplicationPools[AppPoolName].ManagedRuntimeVersion = "v4.0";
                sm.ApplicationPools[AppPoolName].ManagedPipelineMode = ManagedPipelineMode.Classic; //托管模式Integrated為集成 Classic為經(jīng)典
                sm.CommitChanges();
                #endregion

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


            //啟動(dòng)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);
            //啟動(dòng)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);
            }

        }

復(fù)制代碼 代碼如下:

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;   // 主機(jī)IP
            private string portNum;   // 網(wǎng)站端口號(hào)
            private string descOfWebSite; // 網(wǎng)站表示。一般為網(wǎng)站的網(wǎng)站名。例如"www.dns.com.cn"
            private string commentOfWebSite;// 網(wǎng)站注釋。一般也為網(wǎng)站的網(wǎng)站名。
            private string webPath;   // 網(wǎng)站的主目錄。例如"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); //網(wǎng)站標(biāo)識(shí)(IP,端口,主機(jī)頭值)
                }
            }

            public string PortNum
            {
                get
                {
                    return portNum;
                }
            }

            public string CommentOfWebSite
            {
                get
                {
                    return commentOfWebSite;
                }
            }

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

8:下面的代碼是對(duì)文件夾權(quán)限進(jìn)行設(shè)置,下面代碼是創(chuàng)建Everyone 并給予全部權(quán)限

復(fù)制代碼 代碼如下:

/// <summary>
        /// 設(shè)置文件夾權(quán)限 處理給EVERONE賦予所有權(quán)限
        /// </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);
        }

相關(guān)文章

  • OpenCvSharp圖像的修改和保存以及掩膜操作

    OpenCvSharp圖像的修改和保存以及掩膜操作

    這篇文章主要介紹了OpenCvSharp圖像的修改和保存以及掩膜操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • C#實(shí)現(xiàn)農(nóng)歷日歷的方法

    C#實(shí)現(xiàn)農(nóng)歷日歷的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)農(nóng)歷日歷的方法,詳細(xì)分析了使用C#實(shí)現(xiàn)農(nóng)歷日歷的完整步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • C#開發(fā)中的垃圾回收機(jī)制簡析

    C#開發(fā)中的垃圾回收機(jī)制簡析

    這篇文章主要為大家詳細(xì)介紹了C#開發(fā)中的垃圾回收機(jī)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2015-10-10
  • C#對(duì)Xamarin框架進(jìn)行數(shù)據(jù)綁定

    C#對(duì)Xamarin框架進(jìn)行數(shù)據(jù)綁定

    這篇文章介紹了C#對(duì)Xamarin框架進(jìn)行數(shù)據(jù)綁定,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-01-01
  • C# 泛型的簡單理解(安全、集合、方法、約束、繼承)分享

    C# 泛型的簡單理解(安全、集合、方法、約束、繼承)分享

    這篇文章介紹了C# 泛型的簡單理解(安全、集合、方法、約束、繼承),有需要的朋友可以參考一下
    2013-10-10
  • 基于C#實(shí)現(xiàn)語音識(shí)別功能詳解

    基于C#實(shí)現(xiàn)語音識(shí)別功能詳解

    在.NET4.0中,可以借助System.Speech組件讓電腦來識(shí)別我們的聲音。本文將利用該組件實(shí)現(xiàn)語音識(shí)別功能,文中實(shí)現(xiàn)過程講解詳細(xì),需要的可以參考一下
    2022-04-04
  • 基于C#的電視臺(tái)節(jié)目表接口調(diào)用代碼

    基于C#的電視臺(tái)節(jié)目表接口調(diào)用代碼

    這篇文章主要介紹了基于C#的電視臺(tái)節(jié)目表接口調(diào)用代碼的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • 基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼

    基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼

    迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試
    2022-03-03
  • C#圖形編程GDI+基礎(chǔ)介紹

    C#圖形編程GDI+基礎(chǔ)介紹

    這篇文章介紹了C#中的圖形編程GDI+,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#創(chuàng)建二叉搜索樹的方法

    C#創(chuàng)建二叉搜索樹的方法

    這篇文章主要介紹了C#創(chuàng)建二叉搜索樹的方法,涉及C#二叉搜索樹的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04

最新評(píng)論