C#操作IIS設定

來源:互聯網
上載者:User

標籤:getc   gac   exe   collect   nis   list   code   gets   rip   

通過C#可以很容易控制IIS的各種屬性。

下面說明如何通過c#操作IIS的“ISAPI和CGI限制”

(一)人工操作

 

 

(二)利用命令

在開始菜單運行裡,輸入cmd,然後輸入  

cd %windir%\system32\inetsrv

 上面命令切換到IIS所安裝的路徑,然後運行 appcmd.exe 這是IIS7極其以後新增的一個命令,IIS6不支援

  然後輸入下面命令,列出所有的ISPAPI

appcmd list config /section:isapiCgiRestriction

 

 

(三)通過C#操作IIS

首先,必須確保電腦上已經安裝了IIS,安裝後,系統預設會註冊一個DLL,例如我的位置是

C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Administration\7.0.0.0__31bf3856ad364e35\Microsoft.Web.Administration.dll

 

添加對該DLL引用。 

 

 

然後就可以擷取了,

代碼為:

            using (ServerManager serverManager = new ServerManager())            {                Configuration config = serverManager.GetApplicationHostConfiguration();                ConfigurationSection isapiCgiRestrictionSection = config.GetSection("system.webServer/security/isapiCgiRestriction");                ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();                foreach (ConfigurationElement element in isapiCgiRestrictionCollection)                {                    string path = element["path"].ToString();                    string allowed = element["allowed"].ToString();                    string description = element["description"].ToString();                    string groupId = element["groupId"].ToString();                                                  }}

 

 

如果監控,就可以看到

 

 

 

你也可以自己添加ISPAPI

               //ConfigurationElement addElement = isapiCgiRestrictionCollection.CreateElement("add");                //addElement["path"] = @"C:\Windows\abc\aspnet_isapi.dll";                //addElement["allowed"] = true;                //addElement["groupId"] = @"ContosoGroup";                //addElement["description"] = @"Contoso Extension";                //isapiCgiRestrictionCollection.Add(addElement);                //serverManager.CommitChanges();

 

C#操作IIS設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.