![](/img/trans.png)
[英]C# combining absolute and relative paths, Path.GetFullPath() inconsistent
[英]C# Combining two relative network paths
System.IO.Path 方法在组合两条路径时给了我一些非常奇怪的结果;
string Dir = "\\server1\\customers\\Test";
string path = "..\\..\\customers\\Test\\hello.pbt";
path = Path.Combine(Dir, path);
// path = "\\server1\\customers\\Test\\..\\..\\customers\\Test\\hello.pbt"
现在我想结合这些路径:
path = "\\server1\\customers\\Test\\hello.pbt" // aim
但是使用 Path.GetFullPath 方法,它不会像它应该的那样返回到服务器
path = Path.GetFullPath(path)
// path = "\\server1\\customers\\customers\\Test\\hello.pbt"
我已经尝试了结合两个相对路径与 C#答案中描述的所有方法
问题似乎是 .NET(也可能是 Windows)没有将\\\\server1\\customers
的父级视为\\\\server
。
从技术上讲,看起来\\\\server
不是有效的 UNC 路径(即您不能直接在那里存储文件)。
var thisWorks = Directory.GetParent(Dir);
var thisIsNull = Directory.GetParent(Directory.GetParent(Dir).FullName);
因此,当您要求..\\\\..\\\\
它实际上会忽略其中之一,因为它推断出它不能在目录树的更高位置...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.