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

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.



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: