Archive for March, 2009

How to locate where a SPWebApplication or a SPSite is stored on the file system

March 11, 2009

private string GetLocalPathOfWebApplication(SPWebApplication webApplication)
string iisConnectionString = “IIS://localhost/W3SVC”;
DirectoryEntry iis = new DirectoryEntry(iisConnectionString);
foreach (DirectoryEntry child in iis.Children)
//filtering web sites
if (child.SchemaClassName == “IIsWebServer”)
DirectoryEntry site = new DirectoryEntry(iisConnectionString + “/” + child.Name);
//comparing web sites names in iis with name of SPWebApplication
if (string.Compare(webApplication.Name, site.Properties[“ServerComment”].Value.ToString(), true) == 0)
//getting local path of the web site
DirectoryEntry virtualDir = new DirectoryEntry(iisConnectionString + “/” + id + “/Root”);
return virtualDir.Properties[“Path”].Value.ToString();
return string.Empty;

Useful for example if you want to backup web.config of the application before you make any changes to it.