0

I have

C:\Softwares\Test\ASPnetTest\Project11\Shared\Public\Images\

How to convert it to

Shared\Public\Images\

Thank you.

Nancy
  • 147
  • 2
  • 18
  • I am retrieving list of directories (physical path) and displaying them as logical paths. – Nancy Nov 13 '11 at 15:47

1 Answers1

2

If you want the path as used in a URL:

You can use something like this (classes are valid in C# too): http://geekswithblogs.net/AlsLog/archive/2006/08/03/87032.aspx

Public Function MapURL(ByVal Path As String) As String
     Dim AppPath As String = _
     HttpContext.Current.Server.MapPath("~")
     Dim url As String = String.Format("~{0}" _
     , Path.Replace(AppPath, "").Replace("\", "/"))
     Return url
End Function

...To make it easy:

private static string MapUrl(string path)
{
    var appPath = HttpContext.Current.Server.MapPath("~");
    return string.Format("~{0}", path.Replace(appPath, "").Replace("\\", "/"));
}

If you want the folder, relative to your app's base path:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.physicalapplicationpath.aspx

Request.PhysicalApplicationPath will give you the c:\softwares\test\aspnettest\project11 part. Then you can get the relative part, see this answer here for that:

How to get relative path from absolute path

Community
  • 1
  • 1
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144