2011-03-25 160 views

回答

12

我強烈建議使用Microsoft.Web.Administration DLL以獲得超級管理功能與IIS。

ServerManager serverManager = new ServerManager(); 

// get the site (e.g. default) 
Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site"); 
// get the application that you are interested in 
Application myApp = site.Applications["/Dev1"]; 

// get the physical path of the virtual directory 
Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath); 

給出:

F:\開發\ \分行DEV1

+0

我發現IIS可以自動生成腳本代碼來獲取某些配置值。在C#中,Shell腳本和JavaScript。但我仍在試圖弄清楚如何獲取虛擬文件夾和Web應用程序的物理路徑。如果有人發現它,你能告訴我嗎?謝謝。 – smwikipedia 2011-03-28 01:43:55

+0

@smwikipedia已添加示例。 – Swaff 2011-03-28 08:56:42

+0

非常感謝。 – smwikipedia 2011-03-29 07:09:54

1

你可以使用WMI這樣的事情:

Using WMI to configure IIS

Administering IIS programatically

而且您可以使用C#中的WMI(Google for「WMI C#」)。

所以,是的,這是可能的。

+0

謝謝你提醒我好老WMI的。這意味着*管理*。我幾乎忘了它。 ;) – smwikipedia 2011-03-25 11:28:53

+0

@smwiki - yes for * management *,但配置更喜歡在IIS6上使用System.DirectoryServices,在IIS7上使用Microsoft.Web.Administration。 – Kev 2011-03-31 15:57:44

0

看看HttpRequest對象。您可以使用HttpContext.Current.Request.Url獲取當前的URL。

如果你想檢查虛擬IIS文件夾是否存在,你可以嘗試對你期望存在的相對URL執行一次Server.MapPath調用(你也可以在HttpContext對象中找到它),如果映射不是成功後,您可以假定您的相對路徑指定的特定子文件夾不存在。

希望這會有所幫助。

相關問題