. net框架是否有任何方法来转换路径(例如。"C:\whatever.txt")转换为文件URI(例如:“文件:/ / / C: / whatever.txt”)?
这个系统。Uri类具有相反的情况(从文件Uri到绝对路径),但就我所能找到的转换为文件Uri而言,没有任何东西。
另外,这不是一个ASP。网络应用程序。
. net框架是否有任何方法来转换路径(例如。"C:\whatever.txt")转换为文件URI(例如:“文件:/ / / C: / whatever.txt”)?
这个系统。Uri类具有相反的情况(从文件Uri到绝对路径),但就我所能找到的转换为文件Uri而言,没有任何东西。
另外,这不是一个ASP。网络应用程序。
当前回答
变通办法很简单。只需使用Uri(). tostring()方法,然后使用百分比编码的空格(如果有的话)。
string path = new Uri("C:\my exampleㄓ.txt").ToString().Replace(" ", "%20");
正确返回文件:///C:/my%20exampleㄓ.txt
其他回答
变通办法很简单。只需使用Uri(). tostring()方法,然后使用百分比编码的空格(如果有的话)。
string path = new Uri("C:\my exampleㄓ.txt").ToString().Replace(" ", "%20");
正确返回文件:///C:/my%20exampleㄓ.txt
这个系统。Uri构造函数能够解析完整的文件路径并将其转换为Uri样式的路径。所以你可以这样做:
var uri = new System.Uri("c:\\foo");
var converted = uri.AbsoluteUri;
VB。NET:
Dim URI As New Uri("D:\Development\~AppFolder\Att\1.gif")
不同的输出:
URI.AbsolutePath -> D:/Development/~AppFolder/Att/1.gif
URI.AbsoluteUri -> file:///D:/Development/~AppFolder/Att/1.gif
URI.OriginalString -> D:\Development\~AppFolder\Att\1.gif
URI.ToString -> file:///D:/Development/~AppFolder/Att/1.gif
URI.LocalPath -> D:\Development\~AppFolder\Att\1.gif
一个衬套:
New Uri("D:\Development\~AppFolder\Att\1.gif").AbsoluteUri
输出:文件:/ / / D: /开发/ ~ AppFolder /丙氨酸/ 1. gif
至少在。net 4.5+中你还可以做到:
var uri = new System.Uri("C:\\foo", UriKind.Absolute);
似乎没有人意识到的是,没有一个系统。Uri构造函数正确地处理带有百分号的某些路径。
new Uri(@"C:\%51.txt").AbsoluteUri;
这给了你“file:///C:/Q.txt”而不是“file:///C:/%2551.txt”。
已弃用的dontEscape参数的值都没有任何区别,指定UriKind也会给出相同的结果。尝试使用UriBuilder也没有帮助:
new UriBuilder() { Scheme = Uri.UriSchemeFile, Host = "", Path = @"C:\%51.txt" }.Uri.AbsoluteUri
这也会返回"file:///C:/Q.txt"。
据我所知,框架实际上缺乏正确执行此操作的任何方法。
我们可以尝试将反斜杠替换为正斜杠,并将路径提供给Uri。EscapeUriString -即。
new Uri(Uri.EscapeUriString(filePath.Replace(Path.DirectorySeparatorChar, '/'))).AbsoluteUri
This seems to work at first, but if you give it the path C:\a b.txt then you end up with file:///C:/a%2520b.txt instead of file:///C:/a%20b.txt - somehow it decides that some sequences should be decoded but not others. Now we could just prefix with "file:///" ourselves, however this fails to take UNC paths like \\remote\share\foo.txt into account - what seems to be generally accepted on Windows is to turn them into pseudo-urls of the form file://remote/share/foo.txt, so we should take that into account as well.
EscapeUriString还有一个问题,它没有转义'#'字符。在这一点上,我们似乎没有其他选择,只能从头开始制作我们自己的方法。这就是我的建议:
public static string FilePathToFileUrl(string filePath)
{
StringBuilder uri = new StringBuilder();
foreach (char v in filePath)
{
if ((v >= 'a' && v <= 'z') || (v >= 'A' && v <= 'Z') || (v >= '0' && v <= '9') ||
v == '+' || v == '/' || v == ':' || v == '.' || v == '-' || v == '_' || v == '~' ||
v > '\xFF')
{
uri.Append(v);
}
else if (v == Path.DirectorySeparatorChar || v == Path.AltDirectorySeparatorChar)
{
uri.Append('/');
}
else
{
uri.Append(String.Format("%{0:X2}", (int)v));
}
}
if (uri.Length >= 2 && uri[0] == '/' && uri[1] == '/') // UNC path
uri.Insert(0, "file:");
else
uri.Insert(0, "file:///");
return uri.ToString();
}
这是故意让+和:不编码,因为这似乎是通常在Windows上做的事情。它也只编码latin1,因为Internet Explorer无法理解文件url中的unicode字符。