Question

I am trying to display all sites from IIS 8 in a simple C# console application (this is just a spike, I will eventually do this in an asp.net MVC 4 application).

I copied the Microsoft.Web.Administration dll (from C:\Windows\System32\inetsrv) to \References and referenced it from there.

On my server, there are 3 sites:

  • Default Web Site
  • webdav
  • Ardent

I am doing the following to list all the sites:

    static void Main(string[] args)
    {
        using (ServerManager serverManager = new ServerManager())
        {
            try
            {
                if (serverManager != null)
                {
                    Console.WriteLine(serverManager.Sites.ToString());
                    foreach (Site site in serverManager.Sites)
                    {
                        Console.WriteLine(site.Name);
                        Console.Read();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.Read();
            }
        }
    }

The problem is that, when I run this on the server, it only displays

Default Web Site

I ran

appcmd list site

and got all three.

Despite this, I would really like to use Microsoft.Web.Administration, as it seems better to do that than running command lines with appcmd from a C# application.

Please share your insight on why this might be happening. Thanks.

Was it helpful?

Solution

As David pointed out in his comment, I made a silly mistake which I failed to observe. I am posting this answer in order to remove the question from the unanswered list.

The mistake was

Console.Read()

inside the foreach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top