How to get website's physical path on IIS 10?

Ionut Dragomir 26 Reputation points
2022-05-19T08:51:38.997+00:00

In my MVC app I want to get website's physical path on IIS server. My website's properties:

203651-image.png

What I have tried is the following:

System.Diagnostics.Debug.WriteLine("MapPath: " + System.Web.Hosting.HostingEnvironment.MapPath("~/server/publish/"));  

The above line is returning the path where my project was created:

MapPath: C:\ProjectVisualStudio\Reporting\CS\ServerSide\server\publish\

So, to get the physical path on IIS server, I was trying to obtain it with:

int iisNumber = 2;  
  
            using (ServerManager serverManager = new ServerManager())  
            {  
                var site = serverManager.Sites.Where(s => s.Id == iisNumber).SingleOrDefault();  
                var applicationRoot = site.Applications.Where(a => a.Path == "~/server/publish/").SingleOrDefault(); //here I get the exception  
            //    var applicationRoot = site.Applications.SingleOrDefault();  
                var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "~/server/publish/").SingleOrDefault();  
                Console.WriteLine(virtualRoot.PhysicalPath);  
                System.Diagnostics.Debug.WriteLine("***************************************path IIS: " + virtualRoot.PhysicalPath + "***************************************");                 
            }  

But I get an exception

System.NullReferenceException: 'Object reference not set to an instance of an object.' site was null.

As you can see in the image, my site has the id = 2. Although running this:

       System.Diagnostics.Debug.WriteLine($"Available sites: {string.Join(", ", serverManager.Sites.Select(s => s.Id))}");    

is returnning:

Available sites: 1
Available sites: 1

What could I do to accomplish this task?

Windows development | Internet Information Services
Developer technologies | ASP.NET | Other
Developer technologies | C#
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,006 Reputation points Volunteer Moderator
    2022-05-19T15:53:02.943+00:00

    if you run your application with IIS, instead of IIS Express, the initial code is correct.

    System.Diagnostics.Debug.WriteLine("MapPath: " + System.Web.Hosting.HostingEnvironment.MapPath("~/server/publish/"));

    will return the IIS virtual directory path, not the IIS express one (which is mapped to the source).


  2. Sam Wu-MSFT 7,561 Reputation points Microsoft External Staff
    2022-05-23T05:29:24.54+00:00

    @Ionut Dragomir

    a => a.Path == "~/server/publish/"

    Maybe there is something wrong with your path, you can try the following code:

    ServerManager serverManager = new ServerManager();    
    
    // get the site (e.g. default)  
    Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site");  
    
    // get the application that you are interested in  
    Application myApp = site.Applications["/Dev1"];  
    
    // get the physical path of the virtual directory  
    Console.WriteLine(myApp.VirtualDirectories[0].PhysicalPath);  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.