Listing out all sites and their document roots in .Net
So, I had a problem today that required me to find all document roots for an IIS server which is trivial (open up IIS manager, silly) on a single box. However, in this case the code would run on random boxes and I would never know what was on there.
Below is the solution I came up with is below. Hopefully it will be helpful to someone else.
Note: I had to include System.DirectoryServices into the references and in the using/imports clause.
DirectoryEntry de = new DirectoryEntry();
de.Path = "IIS://LocalHost/W3SVC";
foreach(DirectoryEntry deTmp in de.Children)
{
// If the server doesn't have a state it is more
// than likely an app pool or something similar so we don't care
if(deTmp.Properties["ServerState"].Value != null)
{
DirectoryEntry deDocRoot = new DirectoryEntry();
deDocRoot.Path = "IIS://LocalHost/W3SVC/" + deTmp.Name + "/ROOT";
if(deDocRoot.Properties["path"].Value != null)
{
string DocRoot = deDocRoot.Properties["path"].Value.ToString();
// Do whatever you want to with the values now...
}
}
}
I hope someone else finds this useful.
Also, the MSDN documentation for these object is here.
0 Comments:
Post a Comment
<< Home