C#组合两条相对的网络路径

C#组合两条相对的网络路径,c#,path,C#,Path,当组合两条路径时,System.IO.Path方法给了我一些非常奇怪的结果 string Dir = "\\server1\\customers\\Test"; string path = "..\\..\\customers\\Test\\hello.pbt"; path = Path.Combine(Dir, path); // path = "\\server1\\customers\\Test\\..\\..\\customers\\T

当组合两条路径时,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"

我已经尝试了答案中描述的所有方法。问题似乎是.NET(可能还有Windows)没有将
\\server1\customers
的父项视为
\\server

从技术上看,
\\server
不是一个服务器(即,您不能直接在那里存储文件)


因此,当您请求
。\\\\\\\\\\\\/code>时,它实际上会忽略其中一个,因为它推断它不能在目录树的更高位置…

合并方法只是将第一个与第二个连接起来。事实上,结果并不奇怪,但这是意料之中的。什么是“组合”的规则会导致预期的结果?是的,我知道,但它应该倒退两次而不是一次!非常感谢你的回答!!您完全正确,
server1
不是有效的UNC路径。我无法在那个“文件夹”中创建任何内容。它帮助我理解了这个问题problem@tony_370不客气。我也不知道,直到那时我自己玩过它并阅读了它。
DirectoryInfo
告诉我服务器路径不存在。是否有任何功能可以在不检查文件夹是否存在的情况下解析my
“.\”
,简单的回答是您需要自己编写@tony_370。
var thisWorks = Directory.GetParent(Dir);

var thisIsNull = Directory.GetParent(Directory.GetParent(Dir).FullName);