本文實(shí)例講述了C#創(chuàng)建IIS虛擬目錄的方法。分享給大家供大家參考。具體分析如下:
DirectoryEntry是.Net給我們的一大禮物,他的名字我們就知道他的功能--目錄入口。使用過ADSI的人都知道操作IIS,WinNT這些時(shí),我們還需要提供他們的Path,操作IIS時(shí),這個(gè)Path的格式為:  
ComputerName:即操作的服務(wù)器的名字,可以是名字也可以是IP,經(jīng)常用的就是localhost  
Service:即操作的服務(wù)器,IIS中有Web,也有FTP,還有SMTP這些服務(wù),我們主要是操作IIS的Web功能,因此此處就是"W3SVC",如果是FTP則應(yīng)是"MSFTPSVC"  
WebSite:一個(gè)IIS服務(wù)中可以包括很多的站點(diǎn),這個(gè)就用于設(shè)置操作的站點(diǎn)。他的值是一個(gè)數(shù)字,默認(rèn)是1,表示缺省站點(diǎn),如果有其它,則從1開始依次類推。 
Directory:不用說,即操作的目錄名稱,一個(gè)站點(diǎn)一般頂層目錄為"ROOT",其它目錄則是他的孩子(Child)。
首先我們獲取一個(gè)站點(diǎn)的頂層目錄(根目錄):
如果我們創(chuàng)建這個(gè)對(duì)象是沒有發(fā)生異常,則表示這個(gè)目錄是真實(shí)存在的。
下面我們來添加新的虛擬目錄,比如我們要加的是"Aspcn":
DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir"); newVirDir.Invoke("AppCreate",true); newVirDir.CommitChanges(); rootfolder.CommitChanges();創(chuàng)建目錄的思路很簡單,即在根目錄的子集(rootfolder.Children)中再添加一條記錄,這里使用的是DirectoryEntries類中的Add方法,它返回的是一個(gè)DirectoryEntry,表示新加入的目錄,第一個(gè)參數(shù)是虛擬目錄的名字,第二個(gè)則是Schema的類名以表明我們加入的目錄類型。然后再使用DirectoryEntry的Invoke方法,調(diào)用ADSI中的"AppCreate"方法將目錄真正創(chuàng)建(似乎不走這一步也可以創(chuàng)建目錄成功,但是為了保險(xiǎn)起見,大家還是用吧),最后便是依次調(diào)用新、根目錄的CommitChanges方法,確認(rèn)此次操作。
在創(chuàng)建新目錄時(shí),我們也可以同時(shí)給這個(gè)目錄的屬性賦值,但是我的實(shí)戰(zhàn)經(jīng)驗(yàn)告訴我,最好不要這樣做,如果創(chuàng)建時(shí)就賦值,將有很多屬性不能賦值成功,比如重要的表示真實(shí)目錄的Path屬性。因此飛刀建議大家最好是先創(chuàng)建目錄,然后再賦值,即更新目錄信息。
更新虛擬目錄
相信大家對(duì)IIS都比較熟悉,了解IIS中一些重要的設(shè)置,如可讀(AccessRead)、可寫(AccessWrite)、可執(zhí)行(AccessExecute)等。這些都可通過對(duì)DirectoryEntry的Properties屬性集合的賦值來實(shí)現(xiàn)。賦值可以通過兩種方式來完成:
第一種是調(diào)用Properties集合的Add方法,如:  
第二種是對(duì)第一個(gè)索引值賦值:  
刪除虛擬目錄
刪除虛擬目錄的方法也很簡單,就是找到你要?jiǎng)h除的虛擬目錄,然后調(diào)用AppDelete方法。
DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir"); de.Invoke("AppDelete",true); rootfolder.CommitChanges(); 還有一種方法,就是調(diào)用Root目錄的Delete方法。
object[] paras = new object[2]; paras[0] = "IIsWebVirtualDir"; //表示操作的是虛擬目錄 paras[1] = "Aspcn"; rootfolder.Invoke("Delete",paras); rootfolder.CommitChanges(); System.DirectoryServices.DirectoryEntriesIIs創(chuàng)建虛擬目錄
using System;using System.Collections.Generic;using System.Text;using System.DirectoryServices;namespace Install_IIS{  class IISManager  {    public IISManager()     {     }     /// <summary>    /// 創(chuàng)建虛擬目錄    /// </summary>    /// <param name="WebSite">服務(wù)器站點(diǎn)名稱</param>    /// <param name="VDirName">虛擬目錄名稱</param>    /// <param name="Path"></param>    /// <param name="RootDir"></param>    /// <param name="chkRead"></param>    /// <param name="chkWrite"></param>    /// <param name="chkExecute"></param>    /// <param name="chkScript"></param>    /// <param name="chkAuth"></param>    /// <param name="webSiteNum">1</param>    /// <param name="serverName">localhost</param>    /// <returns></returns>    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;      //       // 確定IIS版本      //       IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");       if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN")         IISUnderNT=true;       else         IISUnderNT=false;       IISSchema.Dispose();      //       // Get the admin object       // 獲得管理權(quán)限      //       IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");       if (IISAdmin == null)        return "IIS 未正常安裝";      if (IISAdmin.Children == null)        return "IIS 可能未啟動(dòng)";      //       // If we're not creating a root directory       // 如果我們不能創(chuàng)建一個(gè)根目錄      //       if (!RootDir)       {         //         // If the virtual directory already exists then delete it         // 如果虛擬目錄已經(jīng)存在則刪除        //        foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)         {           if (v.Name == VDirName)           {             // Delete the specified virtual directory if it already exists             try             {             IISAdmin.Invoke("Delete", new string [] { v.SchemaClassName, VDirName });             IISAdmin.CommitChanges();             }             catch(Exception ex)             {             sRet+=ex.Message;             }           }         }       }        //       // Create the virtual directory       // 創(chuàng)建一個(gè)虛擬目錄      //       if (!RootDir)       {         VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");       }       else       {         VDir = IISAdmin;       }      //       // Make it a web application       // 創(chuàng)建一個(gè)web應(yīng)用      //       VDir.Properties["Path"][0] = Path;      //設(shè)置虛擬目錄指向的物理路徑      if (IISUnderNT)       {         VDir.Invoke("AppCreate", false);       }       else       {         VDir.Invoke("AppCreate", 1);       }       //       // Setup the VDir       // 設(shè)置虛擬目錄      //       VDir.Properties["AccessRead"][0] = chkRead; //設(shè)置讀取權(quán)限      VDir.Properties["AccessExecute"][0] = chkExecute; //設(shè)置執(zhí)行權(quán)限      VDir.Properties["AccessWrite"][0] = chkWrite; //設(shè)置寫入權(quán)限      VDir.Properties["AccessScript"][0] = chkScript; //執(zhí)行權(quán)限      VDir.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";//設(shè)置默認(rèn)文檔,多值情況下中間用逗號(hào)分割      VDir.Properties["AppFriendlyName"][0] = VDirName; //應(yīng)用程序名稱      VDir.Properties["AuthFlags"][0] = 0;  //  設(shè)置目錄的安全性,0表示不允許匿名訪問,1為允許,3為基本身份驗(yàn)證,7為windows繼承身份驗(yàn)證      VDir.Properties["AuthNTLM"][0] = chkAuth;       VDir.Properties["EnableDefaultDoc"][0] = true;       VDir.Properties["EnableDirBrowsing"][0] = false;       //       // NT doesn't support this property       // NT格式不支持這特性      //       if (!IISUnderNT)       {         VDir.Properties["AspEnableParentPaths"][0] = true;       }      //       // Set the changes       // 設(shè)置改變      //       VDir.CommitChanges();      //下面的方法是得到所有屬性名稱的方法:      foreach (PropertyValueCollection pvc in VDir.Properties)      {        Console.WriteLine(pvc.PropertyName);      }      sRet+= "VRoot " +VDirName + " created!";       return sRet;     }     #region Properties     public string ServerName     {     get     {     return _serverName;     }     set     {     _serverName = value;     }     }     #endregion    public static string VirDirSchemaName = "IIsWebVirtualDir";    #region Private Members      private string _serverName;    #endregion   } }測(cè)試用:
這個(gè)我已投入項(xiàng)目中使用,可放心使用。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選