views:

47

answers:

3

I am trying to create a list of all virtual directories within an IIS site. However, I have found that trying to do this varies dramatically in the older versions of IIS. In IIS 7 this is a relatively easy task via C# but I can't seem to find a good method for doing this in IIS 6 and 5.

I have tried using the System.DirectoryServices.DirectoryEntry but that doesn't seem to give me the desired output.

I am the server administrator so I'm open to using other things such as .vbs files that are built into IIS as well as writing my own code.

A: 

have you tried using GetObject("IIS://ServerName/W3SVC")

You do it in VBS like this

'Get the IIS Server Object
Set oW3SVC = GetObject("IIS://ServerName/W3SVC/1/ROOT")

For Each oVirtualDirectory In oW3SVC

    Set oFile = CreateObject("Scripting.FileSystemObject")
    Set oTextFile = oFile.OpenTextFile("C:\Results.txt", 8, True)

    oTextFile.WriteLine(oVirtualDirectory.class + " -" + oVirtualDirectory.Name)
    oTextFile.Close

Next
Set oTextFile = Nothing
Set oFile = Nothing

Wscript.Echo "Done"

I have an article about it here -> http://anyrest.wordpress.com/2010/02/10/how-to-list-all-websites-in-iis/

Raymund
This lists the websites under IIS. I'm trying to get all the virtual directories under 1 site.
Avitus
I edited my answer so that it lists the directories and what type of directory it is. Hope this works
Raymund
A: 

Here are two examples that should work across IIS 5, 6 and 7 (with IIS 6 WMI compatibility installed). I successfully tested both with IIS 5 and 7.

VBScript version

Function ListVirtualDirectories(serverName, siteId) 
    Dim webSite 
    Dim webDirectory

    On Error Resume Next 

    Set webSite = GetObject( "IIS://" & serverName & "/W3SVC/" & siteId & "/ROOT" ) 
    If ( Err <> 0 ) Then 
        Err = 0 
        Exit Function 
    Else 
        For Each webDirectory in webSite
            If webDirectory.Class = "IIsWebVirtualDir" Then 
                WScript.Echo "Found virtual directory " & webDirectory.Name
            End If 
        Next
    End If   
End Function

C# version

void ListVirtualDirectories(string serverName, int siteId)
{            
       DirectoryEntry webService = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteId + "/ROOT");

       foreach (DirectoryEntry webDir in webService.Children)
       {
           if (webDir.SchemaClassName.Equals("IIsWebVirtualDir"))
               Console.WriteLine("Found virtual directory {0}", webDir.Name);
       }
}
Garett
A: 

I know you're asking for VB solutions, I don't really know VB, I'm more of a C# person. Anyway, here is a little bit of C#.NET code taken from an app I wrote some time ago which lists the IIS virtual directories...

    private DirectoryEntry _iisServer = null;
    private DirectoryEntry iisServer
    {
        get
        {
            if (_iisServer == null)
            {
                string path = string.Format("IIS://{0}/W3SVC/1", serverName);
                _iisServer = new DirectoryEntry(path);
            }
            return _iisServer;
        }
    }

    private IDictionary<string, DirectoryEntry> _virtualDirectories = null;
    private IDictionary<string, DirectoryEntry> virtualDirectories
    {
        get
        {
            if (_virtualDirectories == null)
            {
                _virtualDirectories = new Dictionary<string, DirectoryEntry>();

                DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName);
                foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
                {
                    _virtualDirectories.Add(virtualDirectory.Name, virtualDirectory);
                }
            }
            return _virtualDirectories;
        }
    }

Hopefully you will be able to get the general idea from that.

Antony Scott