我在ASP.NET领域寻找类似Server.MapPath的东西,将Assembly.GetExecutingAssembly().CodeBase的输出转换为带驱动器号的文件路径.
以下代码适用于我尝试过的测试用例:
private static string ConvertUriToPath(string fileName) { fileName = fileName.Replace("file:///", ""); fileName = fileName.Replace("/", "\\"); return fileName; }
看起来.NET Framework中应该有更好的东西 - 我只是无法找到它.
尝试查看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); }