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

前端 未结 3 1791
花落未央
花落未央 2020-12-24 07:16

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

I guess it\'s something to do with Microsoft.Web.Administration.Se

相关标签:
3条回答
  • 2020-12-24 07:31

    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.

    0 讨论(0)
  • 2020-12-24 07:52

    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)

    0 讨论(0)
  • 2020-12-24 07:57

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

    0 讨论(0)
提交回复
热议问题