In my MVC app I want to get website's physical path on IIS server. This old post is about it, but I'm using a newer version of IIS (10.0.19041.1). Is there another way of accomplishing this task?
I was trying the accepted solution from the above mentioned post, but it throws me an exception System.NullReferenceException: 'Object reference not set to an instance of an object.' site was null..
using Microsoft.Web.Administration;
//...
    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 == "/").SingleOrDefault(); //here I get the exception
            var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").SingleOrDefault();
            Console.WriteLine(virtualRoot.PhysicalPath);
            System.Diagnostics.Debug.WriteLine("***************************************path to IIS website: " + virtualRoot.PhysicalPath + "***************************************");
        }
