2010-01-22 87 views
4

我們有一個安裝在Windows 2003和Windows 2008系統的Web應用程序。在過去,我們的安裝使用ADSI創建一對情侶在IIS應用程序目錄的代碼,但是這需要安裝在Windows 2008年我一直在嘗試使用WMI來創建應用程序目錄中的IIS 6管理組件,所以我們可以支持兩個操作系統。使用WMI創建IIS應用程序目錄與C#

我一直在嘗試這種代碼

public static void AddVirtualFolder(string serverName, string websiteId, string name, string path) 
    { 
     ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", serverName)); 
     scope.Connect(); 

     string siteName = string.Format("W3SVC/{0}/Root/{1}", websiteId, name); 

     ManagementClass mc = new ManagementClass(scope, new ManagementPath("IIsWebVirtualDirSetting"), null); 
     ManagementObject oWebVirtDir = mc.CreateInstance(); 

     oWebVirtDir.Properties["Name"].Value = siteName; 
     oWebVirtDir.Properties["Path"].Value = path; 
     oWebVirtDir.Properties["AuthFlags"].Value = 5; // Integrated Windows Auth. 
     oWebVirtDir.Properties["EnableDefaultDoc"].Value = true; 
     // date, time, size, extension, longdate ; 
     oWebVirtDir.Properties["DirBrowseFlags"].Value = 0x4000003E; 
     oWebVirtDir.Properties["AccessFlags"].Value = 513; // read script 
     oWebVirtDir.Put(); 

     ManagementObject mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDir='" + siteName + "'"), null); 
     ManagementBaseObject inputParameters = mo.GetMethodParameters("AppCreate2"); 
     inputParameters["AppMode"] = 2; 
     mo.InvokeMethod("AppCreate2", inputParameters, null); 
     mo = new ManagementObject(scope, new System.Management.ManagementPath("IIsWebVirtualDirSetting='" + siteName + "'"), null); 
     mo.Properties["AppFriendlyName"].Value = name; 
     mo.Put(); 
    } 
} 

不過,我得到路徑上稱爲目錄未發現的錯誤。如果任何人有一些我可以使用的參考資料,我將不勝感激。任何其他建議如何去做這件事也是受歡迎的。

回答

3

使用上面的代碼,你仍然需要在Windows 2008/IIS7在IIS6兼容性位。這樣做的原因是,調用設置屬性,如DirBrowseFlagsAccessFlags等等是不是在IIS7沒有IIS6管理組件支持IIS 6元數據庫屬性。

對於IIS7我建議直接對Microsoft.Web.Administration命名空間編程,但如果你真的需要使用WMI再看看這篇文章:

Managing Sites with IIS 7.0's WMI Provider (IIS.NET)

+0

我本來希望能夠使用IIS6或IIS7的單一代碼路徑,所以我避免使用IIS7特定的Microsoft.Web.Administration類。但是,我想我只需要檢測IIS版本並繼續使用ADSI for IIS6,並在IIS7的另一個代碼路徑中使用新的東西。 謝謝, – 2010-01-26 13:17:06

+0

@Shane - 另一個很好的理由是,IIS6 compat的墊片還用'不能利用的功能,如預啓動條件AboCustomMapper'對象bodges的'HandlerMappings'(類似於IIS6腳本映射)。相信我,這個努力是值得的,以保持良好的'applicationHost.config'文件衛生。 – Kev 2010-01-26 14:15:52

+0

謝謝,正是我之後 – 2010-05-05 12:46:22

相關問題