20

Is there a way to list all active websites that exist within IIS using c#?

Thanks

Sp

Steven
  • 2,148
  • 8
  • 33
  • 50

3 Answers3

30

For IIS 7 you can use Microsoft.Web.Administration.dll (C:\Windows\System32\inetsrv) and the following code

        var iisManager = new ServerManager();
        SiteCollection sites = iisManager.Sites;
Sir Hally
  • 2,318
  • 3
  • 31
  • 48
  • 4
    This works great, thanks! However, it should be noted that the executing application (VS or the app itself) will need elevated permissions to run this (at least on my Windows 7 setup). – nateirvin Feb 14 '13 at 16:11
  • This was the best thing I found on the internet in a long time(work related of course!) – jackncoke Mar 17 '15 at 13:59
16

Here's an article explaining how this could be done using classes from the System.DirectoryServices namespace as well as the Microsoft.Web.Administration namespace which was introduced with IIS 7.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-9

If anyone is intrested i used the following

var files = Directory.GetFiles(directory, "*",SearchOption.AllDirectories);
sizeInBytes = (from file in files let fileInfo = new FileInfo(file) select fileInfo.Length).Sum();
Steven
  • 2,148
  • 8
  • 33
  • 50
  • 1
    This snippet assumes that directories within the specified directory represents actual websites, which isn't necessarily true. My guess is that you're actually trying to list virtual folders within a site? Rather use the classes within the System.DirectoryServices, it will be more accurate. – cstruter Jul 14 '11 at 10:48