国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#創建IIS虛擬目錄的方法

2019-10-29 21:42:21
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了C#創建IIS虛擬目錄的方法,實例分析了C#在IIS服務器上創建虛擬目錄的相關技巧,需要的朋友可以參考下
 

本文實例講述了C#創建IIS虛擬目錄的方法。分享給大家供大家參考。具體分析如下:

DirectoryEntry是.Net給我們的一大禮物,他的名字我們就知道他的功能--目錄入口。使用過ADSI的人都知道操作IIS,WinNT這些時,我們還需要提供他們的Path,操作IIS時,這個Path的格式為:  

復制代碼代碼如下:
IIS://ComputerName/Service/Website/Directory

 

ComputerName:即操作的服務器的名字,可以是名字也可以是IP,經常用的就是localhost  
Service:即操作的服務器,IIS中有Web,也有FTP,還有SMTP這些服務,我們主要是操作IIS的Web功能,因此此處就是"W3SVC",如果是FTP則應是"MSFTPSVC"  
WebSite:一個IIS服務中可以包括很多的站點,這個就用于設置操作的站點。他的值是一個數字,默認是1,表示缺省站點,如果有其它,則從1開始依次類推。 
Directory:不用說,即操作的目錄名稱,一個站點一般頂層目錄為"ROOT",其它目錄則是他的孩子(Child)。

首先我們獲取一個站點的頂層目錄(根目錄):

復制代碼代碼如下:
DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");

 

如果我們創建這個對象是沒有發生異常,則表示這個目錄是真實存在的。

下面我們來添加新的虛擬目錄,比如我們要加的是"Aspcn":
 

  1. DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn","IIsWebVirtualDir");  
  2. newVirDir.Invoke("AppCreate",true);  
  3. newVirDir.CommitChanges();  
  4. rootfolder.CommitChanges(); 
?

創建目錄的思路很簡單,即在根目錄的子集(rootfolder.Children)中再添加一條記錄,這里使用的是DirectoryEntries類中的Add方法,它返回的是一個DirectoryEntry,表示新加入的目錄,第一個參數是虛擬目錄的名字,第二個則是Schema的類名以表明我們加入的目錄類型。然后再使用DirectoryEntry的Invoke方法,調用ADSI中的"AppCreate"方法將目錄真正創建(似乎不走這一步也可以創建目錄成功,但是為了保險起見,大家還是用吧),最后便是依次調用新、根目錄的CommitChanges方法,確認此次操作。 

在創建新目錄時,我們也可以同時給這個目錄的屬性賦值,但是我的實戰經驗告訴我,最好不要這樣做,如果創建時就賦值,將有很多屬性不能賦值成功,比如重要的表示真實目錄的Path屬性。因此飛刀建議大家最好是先創建目錄,然后再賦值,即更新目錄信息。

更新虛擬目錄 

相信大家對IIS都比較熟悉,了解IIS中一些重要的設置,如可讀(AccessRead)、可寫(AccessWrite)、可執行(AccessExecute)等。這些都可通過對DirectoryEntry的Properties屬性集合的賦值來實現。賦值可以通過兩種方式來完成: 

第一種是調用Properties集合的Add方法,如:  

復制代碼代碼如下:
dir.Properties["AccessRead"].Add(true);

 

第二種是對第一個索引值賦值:  

復制代碼代碼如下:
dir.Properties["AccessRead"][0] = true;

這兩種方法都是可行的。具體是要看你的喜好了。  
在進行賦值之前我們還是要確定要要賦值的目標吧:)這里我們使用DirectoryEntries類的Find方法,如:  
復制代碼代碼如下:
DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");

找到了,我們就可以賦值了。賦值時一定要好好看看啊,虛擬目錄的屬性值可以超多,一查一大堆。。:(太多了,飛刀我也不重復了,大家去微軟的站點上查:)  
比較常用的有:
AccessRead,AccessWrite,AccessExecute,AccessScript,DefaultDoc,EnableDefaultDoc,Path

 

刪除虛擬目錄

刪除虛擬目錄的方法也很簡單,就是找到你要刪除的虛擬目錄,然后調用AppDelete方法。
 

  1. DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");  
  2. de.Invoke("AppDelete",true);  
  3. rootfolder.CommitChanges();  
?

還有一種方法,就是調用Root目錄的Delete方法。
 

  1. object[] paras = new object[2];  
  2. paras[0] = "IIsWebVirtualDir"//表示操作的是虛擬目錄  
  3. paras[1] = "Aspcn";  
  4. rootfolder.Invoke("Delete",paras);  
  5. rootfolder.CommitChanges();  
  6. System.DirectoryServices.DirectoryEntries 
?

IIs創建虛擬目錄
 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.DirectoryServices; 
  5. namespace Install_IIS 
  6.   class IISManager 
  7.   { 
  8.     public IISManager()  
  9.     {  
  10.     }  
  11.     /// <summary> 
  12.     /// 創建虛擬目錄 
  13.     /// </summary> 
  14.     /// <param name="WebSite">服務器站點名稱</param> 
  15.     /// <param name="VDirName">虛擬目錄名稱</param> 
  16.     /// <param name="Path"></param> 
  17.     /// <param name="RootDir"></param> 
  18.     /// <param name="chkRead"></param> 
  19.     /// <param name="chkWrite"></param> 
  20.     /// <param name="chkExecute"></param> 
  21.     /// <param name="chkScript"></param> 
  22.     /// <param name="chkAuth"></param> 
  23.     /// <param name="webSiteNum">1</param> 
  24.     /// <param name="serverName">localhost</param> 
  25.     /// <returns></returns> 
  26.     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)  
  27.     {  
  28.       string sRet=String.Empty;  
  29.       System.DirectoryServices.DirectoryEntry IISSchema;  
  30.       System.DirectoryServices.DirectoryEntry IISAdmin;  
  31.       System.DirectoryServices.DirectoryEntry VDir;  
  32.       bool IISUnderNT; 
  33.       //  
  34.       // 確定IIS版本 
  35.       //  
  36.       IISSchema = new System.DirectoryServices.DirectoryEntry("IIS://" + serverName + "/Schema/AppIsolated");  
  37.       if(IISSchema.Properties["Syntax"].Value.ToString().ToUpper()=="BOOLEAN")  
  38.         IISUnderNT=true;  
  39.       else 
  40.         IISUnderNT=false;  
  41.       IISSchema.Dispose(); 
  42.       //  
  43.       // Get the admin object  
  44.       // 獲得管理權限 
  45.       //  
  46.       IISAdmin=new System.DirectoryServices.DirectoryEntry("IIS://" +serverName +"/W3SVC/" + webSiteNum + "/Root"); 
  47.   
  48.       if (IISAdmin == null
  49.         return "IIS 未正常安裝"
  50.       if (IISAdmin.Children == null
  51.         return "IIS 可能未啟動"
  52.       //  
  53.       // If we're not creating a root directory  
  54.       // 如果我們不能創建一個根目錄 
  55.       //  
  56.       if (!RootDir)  
  57.       {  
  58.         //  
  59.         // If the virtual directory already exists then delete it  
  60.         // 如果虛擬目錄已經存在則刪除 
  61.         // 
  62.         foreach(System.DirectoryServices.DirectoryEntry v in IISAdmin.Children)  
  63.         {  
  64.           if (v.Name == VDirName)  
  65.           {  
  66.             // Delete the specified virtual directory if it already exists  
  67.             try 
  68.             {  
  69.             IISAdmin.Invoke("Delete"new string [] { v.SchemaClassName, VDirName });  
  70.             IISAdmin.CommitChanges();  
  71.             }  
  72.             catch(Exception ex)  
  73.             {  
  74.             sRet+=ex.Message;  
  75.             }  
  76.           }  
  77.         }  
  78.       }   
  79.       //  
  80.       // Create the virtual directory  
  81.       // 創建一個虛擬目錄 
  82.       //  
  83.       if (!RootDir)  
  84.       {  
  85.         VDir = IISAdmin.Children.Add(VDirName, "IIsWebVirtualDir");  
  86.       }  
  87.       else 
  88.       {  
  89.         VDir = IISAdmin;  
  90.       } 
  91.       //  
  92.       // Make it a web application  
  93.       // 創建一個web應用 
  94.       //  
  95.       VDir.Properties["Path"][0] = Path; 
  96.       //設置虛擬目錄指向的物理路徑 
  97.       if (IISUnderNT)  
  98.       {  
  99.         VDir.Invoke("AppCreate"false);  
  100.       }  
  101.       else 
  102.       {  
  103.         VDir.Invoke("AppCreate", 1);  
  104.       }  
  105.       //  
  106.       // Setup the VDir  
  107.       // 設置虛擬目錄 
  108.       //  
  109.       VDir.Properties["AccessRead"][0] = chkRead; //設置讀取權限 
  110.       VDir.Properties["AccessExecute"][0] = chkExecute; //設置執行權限 
  111.       VDir.Properties["AccessWrite"][0] = chkWrite; //設置寫入權限 
  112.       VDir.Properties["AccessScript"][0] = chkScript; //執行權限 
  113.       VDir.Properties["DefaultDoc"][0] = "index.asp,Default.aspx";//設置默認文檔,多值情況下中間用逗號分割 
  114.       VDir.Properties["AppFriendlyName"][0] = VDirName; //應用程序名稱 
  115.       VDir.Properties["AuthFlags"][0] = 0;  //  設置目錄的安全性,0表示不允許匿名訪問,1為允許,3為基本身份驗證,7為windows繼承身份驗證 
  116.       VDir.Properties["AuthNTLM"][0] = chkAuth;  
  117.       VDir.Properties["EnableDefaultDoc"][0] = true;  
  118.       VDir.Properties["EnableDirBrowsing"][0] = false;  
  119.       //  
  120.       // NT doesn't support this property  
  121.       // NT格式不支持這特性 
  122.       //  
  123.       if (!IISUnderNT)  
  124.       {  
  125.         VDir.Properties["AspEnableParentPaths"][0] = true;  
  126.       } 
  127.       //  
  128.       // Set the changes  
  129.       // 設置改變 
  130.       //  
  131.       VDir.CommitChanges(); 
  132.       //下面的方法是得到所有屬性名稱的方法: 
  133.       foreach (PropertyValueCollection pvc in VDir.Properties) 
  134.       { 
  135.         Console.WriteLine(pvc.PropertyName); 
  136.       } 
  137.       sRet+= "VRoot " +VDirName + " created!";  
  138.       return sRet;  
  139.     } 
  140.      #region Properties  
  141.     public string ServerName  
  142.     {  
  143.     get 
  144.     {  
  145.     return _serverName;  
  146.     }  
  147.     set 
  148.     {  
  149.     _serverName = value;  
  150.     }  
  151.     }  
  152.     #endregion 
  153.     public static string VirDirSchemaName = "IIsWebVirtualDir"
  154.     #region Private Members   
  155.     private string _serverName; 
  156.     #endregion  
  157.   }  
?

測試用:

復制代碼代碼如下:
MessageBox.Show(new IISManager().CreateVDir("localhost", "ietm", "c://myweb", false, true, false, false, true, false, 1, "localhost"));

 

這個我已投入項目中使用,可放心使用。

希望本文所述對大家的C#程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南川市| 大竹县| 和静县| 保山市| 永兴县| 客服| 湘阴县| 门源| 韶山市| 太保市| 乐清市| 绥棱县| 浙江省| 徐闻县| 安顺市| 唐河县| 赞皇县| 耒阳市| 曲沃县| 静乐县| 元阳县| 建瓯市| 太原市| 新宁县| 西安市| 邵阳市| 汨罗市| 湖北省| 彭泽县| 拜泉县| 静海县| 临夏县| 建平县| 新巴尔虎右旗| 金华市| 石台县| 新疆| 赤壁市| 交城县| 上虞市| 芜湖县|