如何从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:\\
}
string path = "file:///D:/Directory/File.txt";
if(Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute)) {
Uri uri = new Uri(path);
string actualPath = uri.AbsolutePath;
}