Path.Combine absolute with relative path strings

后端 未结 7 1293
旧时难觅i
旧时难觅i 2020-11-27 05:08

I\'m trying to join a Windows path with a relative path using Path.Combine.

However, Path.Combine(@\"C:\\blah\",@\"..\\bling\") returns C:\\blah\\

7条回答
  •  情深已故
    2020-11-27 05:28

    Be careful with Backslashes, don't forget them (neither use twice:)

    string relativePath = "..\\bling.txt";
    string baseDirectory = "C:\\blah\\";
    //OR:
    //string relativePath = "\\..\\bling.txt";
    //string baseDirectory = "C:\\blah";
    //THEN
    string absolutePath = Path.GetFullPath(baseDirectory + relativePath);
    

提交回复
热议问题