24

How to get the path that usually looks like %SystemDrive%\inetpub\wwwroot ?

I guess it's something to do with Microsoft.Web.Administration.ServerManager class, but I couldn't find a way.

Update: I'm trying to get the path from standalone app. Not an ASP.NET web app.

Kev
  • 118,037
  • 53
  • 300
  • 385
iLemming
  • 34,477
  • 60
  • 195
  • 309
  • Are you trying to do this from within the application itself -- ie, ASP.NET -- or some sort of standalone application? – LukeH Jul 12 '10 at 13:18
  • its better you ask question properly with all information and mark your updated in bold as i did in my answer – Pranay Rana Jul 12 '10 at 18:49
  • I had this same problem recently: http://pmichaels.net/2016/02/26/how-to-programmatically-retrieve-the-physical-path-from-an-iis-site/ – Paul Michaels Feb 28 '16 at 10:21

3 Answers3

47

To discover the physical path of a website from a standalone application you can do the following:

// If IIS7
// Add reference to Microsoft.Web.Administration in 
// C:\windows\system32\inetsrv

using Microsoft.Web.Administration;
...

int iisNumber = 2;

using(ServerManager serverManager = new ServerManager())
{
  var site = serverManager.Sites.Where(s => s.Id == iisNumber).Single();
  var applicationRoot = 
           site.Applications.Where(a => a.Path == "/").Single();
  var virtualRoot = 
           applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single();
  Console.WriteLine(virtualRoot.PhysicalPath);
}

If you're using IIS 6 (or the IIS6 admin compatibility layer for IIS7)

// If IIS6
// Add reference to System.DirectoryServices on .NET add ref tab

using System.DirectoryServices;
...

int iisNumber = 2;

string metabasePath = String.Format("IIS://Localhost/W3SVC/{0}/root", iisNumber);
using(DirectoryEntry de = new DirectoryEntry(metabasePath))
{
  Console.WriteLine(de.Properties["Path"].Value);
}

Both these examples demonstrate how to discover the path to the root of a Web Site.

To discover the path to a virtual directory you need to amend the paths as necessary.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 1
    This won't work if your "App" is running in the SQL CLR, since neither that Microsoft.Web.Administration.dll nor its endless chain of dependencies have been tested in SQL Server. Is there some alternative, more basic way of finding web application paths, or some other DLL that is less feature-packed? – Triynko Aug 01 '13 at 01:00
  • how does one get, Microsoft.Web.Administration.. I have Win7 and I don't have that DLL anywhere on my machine. Microsoft.Web.Administration.dll. I did find a Microsoft.Web.Administration.Resource.dll, but that isn't the same file. – Chizl Aug 29 '13 at 17:57
  • @Chizl - it's in `c:\windows\system32\inetsrv` you need to browse to the dll (`Microsoft.Web.Administration.dll`) when adding a reference. – Kev Aug 29 '13 at 18:05
  • @Triynko - sorry, I missed your comment. You could try and parse `c:\windows\system32\inetsrv\config\applicationHost.config`. That's where IIS7 stores its settings. It's XML and shouldn't be hard to do with the right XPath expression. Look in the `` element, there's a `` node for each website. – Kev Aug 29 '13 at 18:11
  • Not sure if this is still valid 3 years later, but several users have noticed that ServerManager has a serious memory link: http://msdn.microsoft.com/en-us/library/microsoft.web.administration.servermanager(v=vs.90).aspx – Johann Dec 13 '13 at 11:56
  • @AndroidDev - can't say that we've ever noticed this. Even if correct, unfortunately `ServerManager` is the only sane way to programmatically manage IIS7. – Kev Dec 13 '13 at 15:23
6

Server.MapPath

or

Request Object Paths Available

RequestObject Property

PhysicalApplicationPath -Returns local file system path of the virtual root for this app. c:\inetpub\wwwroot\webstore

PhysicalPath -Returns the local file system path to the current script or path. c:\inetpub\wwwroot\webstore\admin\paths.aspx

Updates

To access iis from the windows application go through this article : Modification of IIS Metabase in C# ( For IIs 6.0,5.0)

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • I'm actually trying to get the path from a standalone wpf app. Not a web app. Is there any way to get that using Microsoft.Web.Administration classes? – iLemming Jul 12 '10 at 13:34
  • its better you ask question properly with all information and mark your updated in bold as i did in my answer – Pranay Rana Jul 12 '10 at 18:50
  • 1
    Microsoft.Web.Administration.ServerManager was a bit of a giveaway though? :) – Kev Jul 12 '10 at 19:20
0

Server.MapPath is not working in shared hosting environment, in this case you can use HostingEnvironment.MapPath.

Artem Koshelev
  • 10,548
  • 4
  • 36
  • 68