转换包括驱动器盘符的绝对路径.NET文件功能的相对路径驱动器、路径、绝对路径、功能

2023-09-03 05:30:16 作者:信徒.

您如何转换驱动器的相对路径,如D:的test.xml成一个函数,如XDocument.Load()将接受一个绝对路径。 D盘可能有D:\数据作为其当前工作目录,例如,所以D:的test.xml意味着D:\数据\的test.xml。我已经尝试过这样的调酒为D:\的test.xml

How can you convert a drive relative path such as D:test.xml into an absolute path that a function such as XDocument.Load() will accept. The D drive could have D:\data as its current working directory, for example, so D:test.xml would mean D:\data\test.xml . I've already tried such concoctions as D:.\test.xml .

下面是错误,我得到的东西,如D:的test.xml:无效的URI:一个DOS路径必须扎根,例如C:\

Here is the error I get for something like D:test.xml: Invalid URI: A Dos path must be rooted, for example, 'c:\'

推荐答案

您可以使用的 GetFullPath 。例如:

You could use GetFullPath. For example:

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