Search code examples
c#iisdirectoryentryiis-metabase

Does IIS Metabase return sites in Id ascending order?


I'm not sure if my question on the face of it makes full sense, so let me try and elaborate. At the moment I try and check if a website already exists in IIS by creating a new DirectoryEntry:

DirectoryEntry IISWebsites = new DirectoryEntry(MetaBasePath); 

MetaBasePath is defined earlier as:

private const string MetaBasePath = "IIS://Localhost/W3SVC";

I check IISWebsites children in a foreach loop and just wondered if this will run through the children in Id order? From what I've read this is actually stored in the DirectoryEntry 'Name' property.

The reason I ask is that if the website name entered by the user in my web setup project isn't found then I want to return the highest id so I can add 1 to it and create a new website with the name supplied by the user.

Having tested this with my IIS it does seem to return it in this order but I need to be sure.

EDIT

I've found the following on Microsoft support (http://support.microsoft.com/kb/240941):

Note that when the metabase is searched for configuration information, it is enumerated from the bottom, or subkey, to top, or node.

This seems to imply that it does do what I think, but it's not 100% clear if it works on site Id as I'm not sure how this relates to the subkey.


Solution

  • The documentation does not specifically define the order as by site ID so it would not be safe to assume it will always be sorted that way (particularly as your current application eventually gets used with new versions of .NET/Windows/IIS in the future).

    Most likely the number of websites is not going to be big enough that enumerating them to find the max would not be a bottleneck.

    Even so, you can run a search for websites and specify the order using DirectorySearcher.Sort.

    Note that in regards to your edit and how configuration information is enumerated, that does not related to sort order. The one sentence taken out of context is not as clear. Read it in context of the whole paragraph and it is clear that the enumeration behavior is related to metabase property inheritance.