使用ADSI來操作IIS的時的路徑

我們使用ADSI來操作IIS的時候,需要提供他們的Path。比如默認本機80端口的默認站點的目錄路徑就是:IIS://localhost/w3svc/1/root

它的格式是:
IIS://ComputerName/Service/Website/Directory

ComputerName:即操作的服務器的名字,可以是名字也可以是IP,經常用的就是localhost

Service:即操作的服務器,IIS中有Web,也有FTP,還有SMTP這些服務,我們主要是操作IIS的Web功能,因此此處就是"W3SVC",如果是FTP則應是"MSFTPSVC"

WebSite:一個IIS服務中可以包括很多的站點,這個就用於設置操作的站點。他的值是一個數字,默認是1,表示缺省站點,如果有其它,也是數字。
但需要注意的是,並不是自增。後面會有一個小程序獲得這個值。

Directory:要操作的目錄名稱,一個站點一般頂層目錄爲"ROOT",其它目錄則是他的孩子(Child)。

 

以上資料摘自飛刀的文章,具體看:
http://aspcool.com/lanmu/browse1.asp?ID=914&bbsuser=csharp
http://aspcool.com/lanmu/browse1.asp?ID=915&bbsuser=csharp


由於上面的WebSite 並不是簡單的自增。我們要知道某臺機子上所有站點對應的值,可以通過下面的小程序獲得這個值:

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

DirectoryEntry root = new DirectoryEntry("IIS://localhost/W3SVC");
foreach(DirectoryEntry dir in root.Children)
{
 if(dir.SchemaClassName == "IIsWebServer")
 {
  string ww = dir.Properties["ServerComment"].Value.ToString();
  Response.Write (string.Format("IIS://localhost/W3SVC/{0}/ROOT/&nbsp;&nbsp;{1}<br>",dir.Name,ww));
 }
}

當然,你想獲得更多的屬性值,可以通過 dir.Properties[] 去獲得。

爲了說明這裏的 WebSite 並不是自增的,下面看我在我本機執行上面程序的結果。

IIS://localhost/W3SVC/1/ROOT/  Default Web Site
IIS://localhost/W3SVC/1307630583/ROOT/  MyWeb81
IIS://localhost/W3SVC/1307630584/ROOT/  MyWeb82
IIS://localhost/W3SVC/1307630585/ROOT/  MyWeb83
IIS://localhost/W3SVC/1307630586/ROOT/  MyWeb84
IIS://localhost/W3SVC/1307630587/ROOT/  MyWeb85
IIS://localhost/W3SVC/1307630683/ROOT/  MyWeb90
IIS://localhost/W3SVC/1307630684/ROOT/  MyWeb91
IIS://localhost/W3SVC/1307630685/ROOT/  MyWeb92
IIS://localhost/W3SVC/1307630686/ROOT/  MyWeb93
IIS://localhost/W3SVC/1758797915/ROOT/  ghj1976.net
IIS://localhost/W3SVC/2/ROOT/  Microsoft SharePoint Administration
IIS://localhost/W3SVC/2546/ROOT/  94

各個的值都不一樣。所以在使用這個路徑的時候,不要想當然的以爲是簡單的自增。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;

namespace cendy
{
 /// <summary>
 /// iis 的摘要說明。
 /// </summary>
 public class iis : System.Web.UI.Page
 {
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此處放置用戶代碼以初始化頁面
   CreateVDir("sohu.com","sho","d://www",true,true,true,true,true,true,1,"cnsun26");

  }
  public string CreateVDir(string WebSite, string VDirName,string Path,bool RootDir,bool chkRead,bool chkWrite,bool chkExecute,bool chkScript,bool chkAuth,int webSiteNum,string serverName)
  {
   string sRet=String.Empty;
   System.DirectoryServices.DirectoryEntry IISSchema;
   System.DirectoryServices.DirectoryEntry IISAdmin;
   System.DirectoryServices.DirectoryEntry VDir;
   bool IISUnderNT;
   IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/Schema/AppIsolated");
   if (IISSchema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN")
    IISUnderNT = true;
   else
    IISUnderNT = false;
   IISSchema.Dispose();
   IISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");

   if (!RootDir)
   {
    
    foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)
    {
     if (v.Name == VDirName)
     {
      
      try
      {
       IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName });
       IISAdmin.CommitChanges();
      }
      catch(Exception ex)
      {
       sRet+=ex.Message;
      }
     }
    }
   }


   if (!RootDir)
   {
    VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
   }
   else
   {
    VDir = IISAdmin;
   }

   VDir.Properties["AccessRead"][0] = chkRead;
   VDir.Properties["AccessExecute"][0] = chkExecute;
   VDir.Properties["AccessWrite"][0] = chkWrite;
   VDir.Properties["AccessScript"][0] = chkScript;
   VDir.Properties["AuthNTLM"][0] = chkAuth;
   VDir.Properties["EnableDefaultDoc"][0] = true;
   VDir.Properties["EnableDirBrowsing"][0] = false;
   VDir.Properties["DefaultDoc"][0] = true;
   VDir.Properties["Path"][0] = Path;


   if (!IISUnderNT)
   {
    VDir.Properties["AspEnableParentPaths"][0] = true;
   }

   VDir.CommitChanges();

   if (IISUnderNT)
   {
    VDir.Invoke("AppCreate", false);
   }
   else
   {
    VDir.Invoke("AppCreate", 1);
   }

   sRet+= "VRoot " +VDirName + " created!";
   return sRet;
  }


  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章