?
Solved

List IIS websites

Posted on 2008-10-14
4
Medium Priority
?
2,614 Views
Last Modified: 2008-10-20
Hi.

I've got a small application that currently offers a list of virtual directories on the local instance of IIS.  This has been fine for all users, until recently.  One has now come along and said that he is not using an installation on localhost, but on a different website (same server, just not localhost).

I've got the following code that currently gives me a list of virtual directories...

        DirectoryEntry iisServer = new DirectoryEntry("IIS://localhost/W3SVC/1");
        DirectoryEntry folderRoot = iisServer.Children.Find("Root", IIsWebVirtualDir);
        return folderRoot.Children;

What I need is two things.

Firstly, I need something that will give me a list of all the local websites under IIS, and secondly, I need to change the above code to suit other websites.  I did originally give the user a new version of the application with his website name hard-coded into the 'serverName' variable, but it didn't work.  This was just a temporary workround, so I'm not too fussed that it worked, but the idea is that the user has a dropdown with websites in it, and when they select one, I populate the virtual directories dropdown.  I've got most of this working.  Just stuck on those 2 points.

I'd appreciate help with this.

Thanks in advance,

John.
0
Comment
Question by:Jaymol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22710315
please have a look at the following link as it provides you with the sample of looping through IIS
http://bytes.com/forum/thread234378.html
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22710325
this link is also good
i tried the sample code and it gave me the listing of all the directories in IIS
http://chiragrdarji.wordpress.com/2007/06/20/createdeleteview-virtual-directory-or-website-in-iis-using-c/
0
 
LVL 6

Author Comment

by:Jaymol
ID: 22719178
Hi Raqi,

Thanks, but both of the sites you directed me to only show how to list virtual directories within the default web site.  My problem is that I need to list the web sites (not just the default web site), and then the virtual directories within each of these.  I'm afraid that neither can tell me anything more than I already know.

Thanks for the comments all the same.

John.
0
 
LVL 6

Accepted Solution

by:
Jaymol earned 0 total points
ID: 22719451
I've finally found the answer to this myself.  I've attached a code snippet for anyone who comes across this page whilst looking for a solution to the same issue I had.

using System;
using System.DirectoryServices;
 
namespace ListWebSites
{
  public class IISAdmin
  {
    public static void EnumerateWebsites()
    {
      try
      {
        DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
 
        foreach (DirectoryEntry de in w3svc.Children)
        {
          if (de.SchemaClassName == "IIsWebServer")
          {
            Console.WriteLine(de.Properties["ServerComment"][0].ToString());
          }
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine("Error in EnumerateWebsites: " + ex.Message);
      }
    }
 
    public static void Main()
    {
      EnumerateWebsites();
      Console.ReadKey(true);
    }
  }
}

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question