C#代碼配置IIS 操縱IIS

來源:互聯網
上載者:User

標籤:

前言:

IIS到目前經曆了四個版本分別為 IIS4.0  IIS5.0 IIS6.0 IIS7.0,其中IIS6.0 IIS7.0是在5.0的安全問題的基礎上獲得的發展,目前為止。6.0版本以後的都是比較安全穩定的,為什麼需要瞭解IIS版本,是因為6.0以後和之 前的IIS提供的操作API是不一樣的,不過IIS6.0時代主要以using System.DirectoryServices空間下的DirectoryEntry 對象作為編程訪問一個主要載體.但隨著IIS7.0發布.NET的Web程式由IIS6.0開始逐漸過渡到 7.0版本.而且在編程式控制制IIS上新添加的Microsoft.Web.Administration名稱空間, 可以操作7.0。

系統要求:親測win7+iis7.0.

以下為代碼部分:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.DirectoryServices;namespace CSIISAdminADSI{    class Program    {        static void Main(string[] args)        {            Console.WriteLine("按任意鍵來建立新的網站。");            Console.ReadLine();            //串連到IIS ADSI提供者                                string ServerName = "LocalHost";            DirectoryEntry W3SVC =                new DirectoryEntry("IIS://" + ServerName + "/w3svc");            // 建立新的Web網站。                        object siteObj;            string siteID = "";            string siteName = "IISADSIDemo";//網站名字            string hostHeader = "www.mytestdomain.com";//網域名稱            string serverBinding1 = ":8181:";            string serverBinding2 = ":8282:" + hostHeader;            string rootPath = "D:\\inetpub\\IISADSIDemo";//路徑            object[] siteProperty =                new object[] {                     siteName,                     new object[] { serverBinding1, serverBinding2 },                     rootPath                 };            try            {                siteObj = (object)W3SVC.Invoke("CreateNewSite", siteProperty);                siteID = siteObj.ToString();                W3SVC.CommitChanges();            }            catch (Exception ex)            {                Console.WriteLine(                    "不能有以下例外建立網站: \n{0}",                    ex.Message);            }            ///////////////////////////////////////////////////////////////////////            // 稍候讓ADSI完成以上操作,然後啟動網站。            Console.WriteLine("網站建立。iisadsidemo開始...");            System.Threading.Thread.Sleep(1000);            DirectoryEntry newSite =                new DirectoryEntry("IIS://" + ServerName + "/W3SVC/" + siteID);            newSite.Invoke("Start");            Console.WriteLine("開始 \n");            Console.WriteLine("建立新的虛擬目錄.");            // 建立新的虛擬目錄。            string metabasePath = string.Format("/W3SVC/{0}/Root", siteID);            DirectoryEntry site =                new DirectoryEntry("IIS://" + ServerName + metabasePath);            DirectoryEntries vdirs = site.Children;            DirectoryEntry newVDir = vdirs.Add("vdir1", "IIsWebVirtualDir");            newVDir.Properties["Path"][0] = "D:\\inetpub\\IISADSIDemo\\vdir1";            newVDir.Properties["AccessScript"][0] = true;            newVDir.Properties["AppFriendlyName"][0] = "vdir1";            newVDir.Properties["AppIsolated"][0] = "1";            newVDir.Properties["AppRoot"][0] = "/LM" + metabasePath + "/vdir1";            newVDir.CommitChanges();            Console.WriteLine("vdir1建立. \n");            // 列出伺服器上的所有網站。                    Console.WriteLine("列出伺服器上的所有網站: \n");            try            {                foreach (DirectoryEntry Site in W3SVC.Children)                {                    if (Site.SchemaClassName == "IIsWebServer")                        Console.WriteLine(Site.Name + " - " +                            Site.Properties["ServerComment"].Value.ToString());                }            }            catch (Exception e)            {                Console.WriteLine(                    "不能有以下例外網站清單: \n{0}: " +                    e.ToString());            }            Console.WriteLine("\nEnd.");            Console.Read();        }    }}
View Code

 

C#代碼配置IIS 操縱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.