How to get the IIS virtual dir & web application's physical paths with C# code?
I am writing a web application deployment verification tool and I need to check the physical folder structures of Web Applicatons and Virtual Folders.
How can this be done in C#? OR more generally, interact with IIS through C# cod开发者_JAVA百科e?
I would strongly suggest using the Microsoft.Web.Administration dll for superb admin features with 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);
Gives:
F:\Dev\Branches\Dev1
You can do this kind of thing using WMI:
Using WMI to configure IIS.
Administering IIS programatically.
And you can use WMI from C# (google for "WMI C#").
So, yes, this is possible.
Take a look at the HttpRequest
object. You can get the current URL using HttpContext.Current.Request.Url
.
If you wanted to check that a virtual IIS folder existed you could try performing a Server.MapPath
call to the relative URL you expect to exist (again you'll find this in the HttpContext
object) and if the mapping is not successful you could then assume that a specific subfolder as specified by your relative path does not exist.
精彩评论