本文實例講述了C#創建IIS虛擬目錄的方法。分享給大家供大家參考。具體分析如下:
DirectoryEntry是.Net給我們的一大禮物,他的名字我們就知道他的功能--目錄入口。使用過ADSI的人都知道操作IIS,WinNT這些時,我們還需要提供他們的Path,操作IIS時,這個Path的格式為:
ComputerName:即操作的服務器的名字,可以是名字也可以是IP,經常用的就是localhost
Service:即操作的服務器,IIS中有Web,也有FTP,還有SMTP這些服務,我們主要是操作IIS的Web功能,因此此處就是"W3SVC",如果是FTP則應是"MSFTPSVC"
WebSite:一個IIS服務中可以包括很多的站點,這個就用于設置操作的站點。他的值是一個數字,默認是1,表示缺省站點,如果有其它,則從1開始依次類推。
Directory:不用說,即操作的目錄名稱,一個站點一般頂層目錄為"ROOT",其它目錄則是他的孩子(Child)。
首先我們獲取一個站點的頂層目錄(根目錄):
如果我們創建這個對象是沒有發生異常,則表示這個目錄是真實存在的。
下面我們來添加新的虛擬目錄,比如我們要加的是"Aspcn":
- DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir");
- newVirDir.Invoke("AppCreate",true);
- newVirDir.CommitChanges();
- rootfolder.CommitChanges();
創建目錄的思路很簡單,即在根目錄的子集(rootfolder.Children)中再添加一條記錄,這里使用的是DirectoryEntries類中的Add方法,它返回的是一個DirectoryEntry,表示新加入的目錄,第一個參數是虛擬目錄的名字,第二個則是Schema的類名以表明我們加入的目錄類型。然后再使用DirectoryEntry的Invoke方法,調用ADSI中的"AppCreate"方法將目錄真正創建(似乎不走這一步也可以創建目錄成功,但是為了保險起見,大家還是用吧),最后便是依次調用新、根目錄的CommitChanges方法,確認此次操作。
在創建新目錄時,我們也可以同時給這個目錄的屬性賦值,但是我的實戰經驗告訴我,最好不要這樣做,如果創建時就賦值,將有很多屬性不能賦值成功,比如重要的表示真實目錄的Path屬性。因此飛刀建議大家最好是先創建目錄,然后再賦值,即更新目錄信息。
更新虛擬目錄
相信大家對IIS都比較熟悉,了解IIS中一些重要的設置,如可讀(AccessRead)、可寫(AccessWrite)、可執行(AccessExecute)等。這些都可通過對DirectoryEntry的Properties屬性集合的賦值來實現。賦值可以通過兩種方式來完成:
第一種是調用Properties集合的Add方法,如:
第二種是對第一個索引值賦值:
刪除虛擬目錄
刪除虛擬目錄的方法也很簡單,就是找到你要刪除的虛擬目錄,然后調用AppDelete方法。
- DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");
- de.Invoke("AppDelete",true);
- rootfolder.CommitChanges();
還有一種方法,就是調用Root目錄的Delete方法。
- object[] paras = new object[2];
- paras[0] = "IIsWebVirtualDir"; //表示操作的是虛擬目錄
- paras[1] = "Aspcn";
- rootfolder.Invoke("Delete",paras);
- rootfolder.CommitChanges();
- System.DirectoryServices.DirectoryEntries
IIs創建虛擬目錄
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.DirectoryServices;
- namespace Install_IIS
- {
- class IISManager
- {
- public IISManager()
- {
- }
- /// <summary>
- /// 創建虛擬目錄
- /// </summary>
- /// <param name="WebSite">服務器站點名稱</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
- // 獲得管理權限
- //
- IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root");
- if (IISAdmin == null)
- return "IIS 未正常安裝";
- if (IISAdmin.Children == null)
- return "IIS 可能未啟動";
- //
- // If we're not creating a root directory
- // 如果我們不能創建一個根目錄
- //
- if (!RootDir)
- {
- //
- // If the virtual directory already exists then delete it
- // 如果虛擬目錄已經存在則刪除
- //
- 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
- // 創建一個虛擬目錄
- //
- if (!RootDir)
- {
- VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");
- }
- else
- {
- VDir = IISAdmin;
- }
- //
- // Make it a web application
- // 創建一個web應用
- //
- VDir.Properties["Path"][0] = Path;
- //設置虛擬目錄指向的物理路徑
- if (IISUnderNT)
- {
- VDir.Invoke("AppCreate", false);
- }
- else
- {
- VDir.Invoke("AppCreate", 1);
- }
- //
- // Setup the VDir
- // 設置虛擬目錄
- //
- VDir.Properties["AccessRead"][0] = chkRead; //設置讀取權限
- VDir.Properties["AccessExecute"][0] = chkExecute; //設置執行權限
- VDir.Properties["AccessWrite"][0] = chkWrite; //設置寫入權限
- VDir.Properties["AccessScript"][0] = chkScript; //執行權限
- VDir.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";//設置默認文檔,多值情況下中間用逗號分割
- VDir.Properties["AppFriendlyName"][0] = VDirName; //應用程序名稱
- VDir.Properties["AuthFlags"][0] = 0; // 設置目錄的安全性,0表示不允許匿名訪問,1為允許,3為基本身份驗證,7為windows繼承身份驗證
- 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
- // 設置改變
- //
- 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#程序設計有所幫助。
新聞熱點
疑難解答