3

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# code?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
smwikipedia
  • 61,609
  • 92
  • 309
  • 482

3 Answers3

14

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

Kevin Brock
  • 8,874
  • 1
  • 33
  • 37
Swaff
  • 13,548
  • 4
  • 26
  • 26
  • I found that IIS can auto-generate the script code to get certain configuration values. In C#, Shell script and javascript. But I am still trying to figure out how to get the physical path of the virtual folder and web app. If anyone found it, could you tell me? thanks. – smwikipedia Mar 28 '11 at 01:43
1

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.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Thanks for reminding me of the good old WMI. It's meant for *management*. I almost forgot it. ;) – smwikipedia Mar 25 '11 at 11:28
  • @smwiki - yes for *management*, but for configuration prefer to use `System.DirectoryServices` on IIS6 and `Microsoft.Web.Administration` on IIS7. – Kev Mar 31 '11 at 15:57
0

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ian
  • 879
  • 8
  • 21