是否有转换文件的URI为路径与驱动器号一个.NET框架的方法?驱动器、路径、框架、文件

2023-09-04 00:27:01 作者:情深何用

我一直在寻找像使用Server.Mappath在ASP.NET境界Assembly.GetExecutingAssembly的输出()。转换codeBase的与驱动器盘符的文件路径。

I was looking for something like Server.MapPath in the ASP.NET realm to convert the output of Assembly.GetExecutingAssembly().CodeBase into a file path with drive letter.

下面code适用于测试情况下,我已经试过:

The following code works for the test cases I've tried:


private static string ConvertUriToPath(string fileName)
{
    fileName = fileName.Replace("file:///", "");
    fileName = fileName.Replace("/", "\\");
    return fileName;
}

好像应该有一些东西在.NET Framework中会好得多 - 我只是一直没能找到它。

It seems like there should be something in the .NET Framework that would be much better--I just haven't been able to find it.

推荐答案

试着在看的 Uri.LocalPath 属性。

private static string ConvertUriToPath(string fileName)
{
   Uri uri = new Uri(fileName);
   return uri.LocalPath;

   // Some people have indicated that uri.LocalPath doesn't 
   // always return the corret path. If that's the case, use
   // the following line:
   // return uri.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
}