开发者

Find Physical Path from Microsoft.Web.Administration and path relative to it's root

开发者 https://www.devze.com 2023-01-27 19:31 出处:网络
I want t开发者_开发技巧o find the physical path within an IIS website, of a path relative to the root of the website, using Microsoft.Web.Administration .NET assembly.

I want t开发者_开发技巧o find the physical path within an IIS website, of a path relative to the root of the website, using Microsoft.Web.Administration .NET assembly.

There doesn't seem to a single method to do this, but I am wondering - is the following procedure the best way?:

  1. Get the Microsoft.Web.Administration.Site object
  2. Find the application with the longest path which matches the beginning of the path relative to the root
  3. Strip the application path from the beginning of the path relative to the root, giving the path relative to the application
  4. Find the virtual directory within this application with the longest path which matches the beginning of the path relative to the application
  5. Strip the application path from the beginning of the path relative to the application, giving the path relative to the virtual directory
  6. Append the path relative to the virtual directory (swapping / for ) to the physical path of the virtual directory to give the physical path we want

Lee


I found the following code to get the path

ServerManager sm = new ServerManager();
sm.Sites["Default Web Site"].Applications["/"].VirtualDirectories["/"].PhysicalPath;

http://forums.iis.net/t/1146686.aspx/1


You are absolutely right, that is the right algorithm. So to paraphrase, make sure to:
1) Find the Site that it matches (in case you are doing it based on host name or binding (ip,port,etc)).
2) Find the Application within the site that matches the longest subset of the remaining URL.
3) Find the VirtualDirectory within the app that matches the longest subset of the remaining URL.
4) Do a Path.Combine(vdir.PhysicalPath, "remaining of the URL with / replaced by \")

That is the physical path. Do remember that nowadays a lot of the URLs could be logical (in scenarios like MVC or Rewriting), so you might not really have a real physical path.


To list all virtual and physical path from a server:

Dim stringIIS As String = String.Empty
Dim serverName As String = "MACHINE_NAME"
Dim sm1 As New ServerManager()

        Using sm As Microsoft.Web.Administration.ServerManager = Microsoft.Web.Administration.ServerManager.OpenRemote(serverName)

            Dim counter As Integer = 1

            For Each Site As Microsoft.Web.Administration.Site In sm.Sites

                stringIIS = "Site: " & Site.Name & (vbNewLine)

                For Each app As Microsoft.Web.Administration.Application In sm.Sites(Site.Name).Applications
                    stringIIS = stringIIS & "        Physical Path: " & (vbTab & sm1.Sites(Site.Name).Applications("/").VirtualDirectories("/").PhysicalPath().ToString() & vbNewLine)

                    For Each virtDir As Microsoft.Web.Administration.VirtualDirectory In app.VirtualDirectories
                        stringIIS = stringIIS & "        Virtual Path: " & (vbTab & app.Path & vbNewLine)
                        stringIIS = stringIIS & vbNewLine
                    Next
                Next

                counter += 1

            Next

            iis.Text = stringIIS
            iis.Visible = True
        End Using
0

精彩评论

暂无评论...
验证码 换一张
取 消