How to get the full path of local IIS root folder?

T

Tee

Hi,

Can anyone tell me how to get the path of IIS root folder from coding?
Eg: C:\Inetpub\wwwroot.

I would like to detect it via coding as not everyone set it at the default
folder.


Thanks,
Tee
 
L

Lenard Gunda

Hi,

I think

Server.MapPath ( "/" );

should work for you. You can run that from within your page. Or if you
want to run it from another class, try:

HttpContext.Current.Server.MapPath ( "/" );

-Lenard
 
T

Tee

Hi,

That's not a ASP .NET project.

It's a winform project and I need to detect user's local IIS folder.


Thanks.
 
A

Andrew Morton

Tee said:
Hi,

That's not a ASP .NET project.

It's a winform project and I need to detect user's local IIS folder.

What do you mean by an IIS folder? If there is more than one site set up
then there may be several folders, possibly on separate disk drives or
computers, containing the served files. Not to mention different virtual
folders in each web site.

Andrew
 
C

Chris Priede

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top