ASP.net获取路径

本文关键字:路径 获取 net ASP | 更新日期: 2023-09-27 18:14:17

我有

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

如何转换为

Shared'Public'Images'

谢谢。

ASP.net获取路径

如果您想要URL中使用的路径:

你可以这样使用(类在c#中也是有效的):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

…为了方便:

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

如果你想要的文件夹,相对于你的应用程序的基本路径:

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

Request.PhysicalApplicationPath会给出c:'softwares'test'aspnettest'project11部分。然后你可以得到相关的部分,看看这里的答案:

如何从绝对路径中获取相对路径