将包含驱动器号的相对路径转换为.NET文件函数的绝对路径

Mic*_*eyn 2 .net c# .net-4.0 c#-4.0

如何将驱动器相对路径(如D:test.xml)转换为XDocument.Load()等函数将接受的绝对路径.例如,D驱动器可以将D:\ data作为其当前工作目录,因此D:test.xml将表示D:\ data\test.xml.我已经尝试过像D:.\ test.xml这样的混合.

这是我得到的类似D的错误:test.xml:无效的URI:Dos路径必须是root,例如'c:\'

Luk*_*keH 5

你可以用GetFullPath.例如:

// should return "D:\data\test.xml" if the current working dir is "D:\data"
string absolutePath = Path.GetFullPath("D:test.xml");
Run Code Online (Sandbox Code Playgroud)