Hi,
Can anyone tell me how to get the path of IIS root folder from coding? Eg:
C:\Inetpub\wwwroot.
First, there may be more than one: Windows Server versions of IIS support
multiple sites. You should enumerate them and ask the user which one they
want to use. Here's some code that does that:
using System.DirectoryServices;
DirectoryEntry iis = new DirectoryEntry("IIS://localhost/W3SVC");
foreach(DirectoryEntry index in iis.Children)
{
if(index.SchemaClassName == "IIsWebServer")
{
int id = Convert.ToInt32(index.Name);
DirectoryEntry site = new DirectoryEntry("IIS://localhost/W3SVC/" +
id);
string siteName = site.Properties["ServerComment"].Value.ToString();
DirectoryEntry rootVDir = new
DirectoryEntry("IIS://localhost/W3SVC/" + id + "/Root");
string rootPath = rootVDir.Properties["Path"].Value.ToString();
// write site name and root path to console
Console.WriteLine("{0}: \"{1}\" -> {2}", id, siteName, rootPath);
}
}
Secondly, you didn't indicate what you are ultimately trying to do, but it
is worth noting that IIS allows multiple virtual directories under each
site. Much of the site content could well be somewhwere else and not under
the root folder.
If your application is supposed to do something with the entire site, you
way wish to enumerate all virtual directories instead of just getting the
root.
On the other hand, if you want to add your web application to an existing
site, you probably should physically install it to any reasonable location
(user selected or together with the rest of your product), then add it as a
new virtual directory programmatically. That would be much nicer than
depositing it somewhere under the site root.
For more information:
http://msdn.microsoft.com/library/en-us/iissdk/html/d39fae66-abe7-4902-a3fc-f36151561f01.asp