Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get website's physical path on local IIS server? (from a desktop app)

Tags:

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.

like image 559
iLemming Avatar asked Jul 12 '10 13:07

iLemming


People also ask

How do I access IIS website locally?

Click Start and type "IIS". Then click "Internet Information Services (IIS) Manager" to open the "Internet Information Services (IIS) Manager". (Alternatively, you can press "Windows + R" to open RUN and type "inetmgr" to open the "Internet Information Services (IIS) Manager").

How do I create a path in IIS?

Solution 1. Open IIS Manager by Typing inetmgr on Start Menu or Run. Click On Sites on the Left navigation of IIS Manager. Right Click on Site where you want to Add Virtual Path and Choose Add Virtual Directory.


1 Answers

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.

like image 131
Kev Avatar answered Oct 09 '22 02:10

Kev