How does IIS generate the Site ID?
So in IIS 6 when you create a new site programmatically you have the option to specify a SiteID or you can let IIS 6 do it for you.
If you let it do it for you it will make this kinda hash of the site name. You have to use this ID when you are doing any modifications to the site (programmatically). So it would be the value in IIS://<servername>/W3SVC/<SiteID>
So how do you get the SiteID?
- Well you could create the site then enumerate it from WMI or ADSI.
- Use my little c# function below to generate the sitename
public static uint GenerateSiteID(string SiteName)
uint id = 0;
char arr = SiteName.ToCharArray(); //Convert the sitename to a Char Array
for (int i = 0; i < arr.Length; i++)
char c = arr[i];
int intc = c;
int upper = intc & '\x00df'; //Upper case the letter
id = (uint)(id * 101) + (uint)upper;
return (id % Int32.MaxValue) + 1; //do a MOD and add 1
Now a good thing to remember is that this might not be the sitename the IIS is using. If there is a SiteID collision then IIS will try to move the ID up by 1 digit then try again. So dont let this be the end all be all of how this is determined.
Using the code above is much faster then parsing through all the sites via ADSI or WMI to get the siteID. So if you have 2000 sites on a server the site was created most recently (bottom of the Metabase) will take longer to query for then the first site created (top of the metabase).
Also note that if you try to convert this code to VB.net you will get an exception becasue VB.net does more bounds checking.