是否有.NETFramework方法将文件URI转换为带盘符的路径?我在ASP.NET世界中寻找类似Server.MapPath的东西,它会Assembly.GetExecutingAssembly()。CodeBase的输出被转换为带有驱动器号的文件路径。以下代码适用于我尝试过的测试用例:privatestaticstringConvertUriToPath(stringfileName){fileName=fileName.Replace("file:///","");fileName=fileName.Replace("/","\");返回文件名;似乎.NETFramework中应该有更好的东西——我只是找不到它。尝试查看Uri.LocalPath属性。privatestaticstringConvertUriToPath(stringfileName){Uriuri=newUri(fileName);返回uri.LocalPath;//有些人指出uri.LocalPath并不//总是返回正确的路径。如果是这种情况,请使用//以下行://returnuri.GetComponents(UriComponents.Path,UriFormat.SafeUnescaped);我找了很多答案,最流行的答案是使用Uri.LocalPath。但是,如果Path包含“#”,System.Uri将无法提供正确的LocalPath。详情在这里。我的解决方案是:privatestaticstringConvertUriToPath(stringfileName){Uriuri=newUri(fileName);返回uri.LocalPath+Uri.UnescapeDataString(uri.Fragment).Replace('/','\');可以使用Assembly.Location吗?该位置可能与CodeBase不同。例如,对于ASP.NET中的文件,它可能会在c:\WINDOWS\Microsoft.NETFrameworkv2.0.50727TemporaryASP.NET下解析。请参考“Assembly.CodeBaseandAssembly.Location”http://blogs.msdn.com/suzcook/archive/2003/06/26/57198.aspx以上是C#学习教程:有没有.NETFramework方法将文件URI转换为带盘符的路径?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
