从URI类型的文件路径中获取驱动器号的最简单方法是什么,例如

file:///D:/Directory/File.txt

我知道我可以做(这里的路径是一个包含上面文本的字符串)
path = path.Replace(@"file:///", String.Empty);
path = System.IO.Path.GetPathRoot(path);

但感觉有点笨拙。有没有一种方法可以不使用String.Replace或类似的东西呢?

最佳答案

var uri = new Uri("file:///D:/Directory/File.txt");
if (uri.IsFile)
{
    DriveInfo di = new DriveInfo(uri.LocalPath);
    var driveName = di.Name; // Result: D:\\
}

关于c# - 来自C#中URI类型文件路径的驱动器号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12685797/

10-11 22:57
查看更多