IIS應用程式集區的使用代碼來管理的方法

來源:互聯網
上載者:User

using System;
using System.DirectoryServices;
using System.Reflection;

namespace ADSI1
{
  ///

  /// Small class containing methods to configure IIS.
  ///

  class ConfigIIS
  {
    ///

    /// The main entry point for the application.
    ///

    [STAThread]
   //主程式入口,可以選擇用哪些,我為了方便,全部功能都寫上去了。
    static void Main(string[] args)
    {
      string AppPoolName = "MyAppPool";
      string newvdir1 = "MyVDir";
      DirectoryEntry newvdir = CreateVDir(newvdir1);

      CreateAppPool(AppPoolName);
      AssignAppPool(newvdir, AppPoolName);

      ConfigAppPool("Stop",AppPoolName);
    }

  //建立虛擬目錄
    static DirectoryEntry CreateVDir (string vdirname)
    {
      DirectoryEntry newvdir;
      DirectoryEntry root=new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
      newvdir=root.Children.Add(vdirname, "IIsWebVirtualDir");
      newvdir.Properties["Path"][0]= "c://inetpub//wwwroot";
      newvdir.Properties["AccessScript"][0] = true;
      newvdir.CommitChanges();
      return newvdir;
    }

//建立新的應用程式集區。
    static void CreateAppPool(string AppPoolName)
    {
      DirectoryEntry newpool;
      DirectoryEntry apppools=new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
      newpool=apppools.Children.Add(AppPoolName, "IIsApplicationPool");
      newpool.CommitChanges();
    }

    static void AssignAppPool(DirectoryEntry newvdir, string AppPoolName)
    {
      object[] param={0, AppPoolName, true};
      newvdir.Invoke("AppCreate3", param);
    }

     //method是管理應用程式池的方法,有三種Start、Stop、Recycle,而AppPoolName是應用程式集區名稱
     static void ConfigAppPool(string method,string AppPoolName)
     {
        DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
        DirectoryEntry findPool = appPool.Children.Find(AppPoolName,IIsApplicationPool");
        findPool.Invoke(method,null);
        appPool.CommitChanges();
        appPool.Close();
     }

      //應用程式集區的列表
      static void AppPoolList()
      {
         DirectoryEntry appPool = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
          foreach(DirectoryEntry a in appPool.Children)
          {
               Console.WriteLine(a.Name);
          } 
      }

     private void VDirToAppPool()
        {
          DirectroryEntry VD = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/ccc");
          Console.WriteLine(VD.Properties["AppPoolId"].Value.ToString());
         }
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.