Here are two helper functions you need to add to your Garetts answer. With their help, you can scroll through each domain and get all its virtual directories, including those that are not in the root folder of the domain.
Get site id from domain name:
string GetSiteID(string domain) { string siteId = string.Empty; DirectoryEntry iis = new DirectoryEntry("IIS://localhost/W3SVC"); foreach (DirectoryEntry entry in iis.Children) if (entry.SchemaClassName.ToLower() == "iiswebserver") if (entry.Properties["ServerComment"].Value.ToString().ToLower() == domain.ToLower()) siteId = entry.Name; if (string.IsNullOrEmpty(siteId)) throw new Exception("Could not find site '" + domain + "'"); return siteId; }
Get all descendant records (recursively) for a site record
static DirectoryEntry[] GetAllChildren(DirectoryEntry entry) { List<DirectoryEntry> children = new List<DirectoryEntry>(); foreach (DirectoryEntry child in entry.Children) { children.Add(child); children.AddRange(GetAllChildren(child)); } return children.ToArray(); }
Site ID mapping for a large number of sites
Edit: after testing with a server containing several hundred domains, GetSiteID () is very slow because it lists all sites again and again to get the identifier. The function below lists sites only once and compares each of them with an identifier and stores it in a dictionary. Then, when you need the site ID, you pull it out of the dictionary, Sites ["domain"]. If you are looking for virtual directories for all sites on the server or a large amount, then the lower will be much faster than GetSiteID () above.
public static Dictionary<string, string> MapSiteIDs() { DirectoryEntry IIS = new DirectoryEntry("IIS://localhost/W3SVC"); Dictionary<string, string> dictionary = new Dictionary<string, string>();
source share