这种制作相对路径的方法有效吗?
由于 .NET 不包含用于创建相对路径的 API,因此我使用了 Uri 的 MakeRelativeUri
方法。这是可行的,但我遇到过一些由于 Uri 被转义而无法实现的情况。所以我也修补了这个问题:
public static string MakeRelativePath(string basePath, string tgtPath) {
return
Uri.UnescapeDataString(
new Uri(basePath, UriKind.Absolute)
.MakeRelativeUri(new Uri(tgtPath, UriKind.Absolute))
.ToString()
).Replace('/', Path.DirectorySeparatorChar);
}
这个版本似乎可以工作,但它让我感到有点疑问:是否有任何有效的本地文件系统路径可能会被这种无端的转义破坏?
相关: 如何从绝对路径获取相对路径路径 该问题的答案根本没有解决异常字符和转义的问题,因此也没有回答这个问题。
Since .NET doesn't include an API to make relative paths, I've used Uri's MakeRelativeUri
method instead. This works, but I've encountered several cases in which it doesn't due to the fact the Uri is escaped. So I've patched up that too:
public static string MakeRelativePath(string basePath, string tgtPath) {
return
Uri.UnescapeDataString(
new Uri(basePath, UriKind.Absolute)
.MakeRelativeUri(new Uri(tgtPath, UriKind.Absolute))
.ToString()
).Replace('/', Path.DirectorySeparatorChar);
}
This versions seems to work, but it leaves me feeling a little quesy: aren't there any valid local filesystem paths that this gratuitous unescaping might corrupt?
Related: How to get relative path from absolute path
The answers to that question do not address the issue of unusual characters and escaping at all, and as such don't answer this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以只使用 System.Uri 和 PathDifference 方法使用的底层算法,而不是转义、取消转义和替换。这是通过 Reflector 恢复的,并经过修改以提高可读性。它还被修改为对 DOS 样式路径使用反斜杠,而不是对 URI 使用正斜杠,并且比较始终不区分大小写。
对输入的期望似乎是,如果任一路径表示目录,则它必须有一个尾部反斜杠。显然,路径也必须不为空。
以下是一些输入和输出示例...看看它是否满足您的需求。
Instead of escaping, unescaping and replacing, you could just use the underlying algorithm used by
System.Uri
and thePathDifference
method. Here it is, recovered via Reflector and modified for slightly better readability. It has also been modified to use backslashes for DOS-style paths instead of forward slashes for URIs, and the comparison is always case-insensitive.The expectation for the inputs seems to be that if either path represents a directory, it must have a trailing backslash. Obviously, the paths must be non-null as well.
Here are some example inputs and outputs... see if it meets your needs.