加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

来自C#的URI类型文件路径的驱动器号

发布时间:2020-12-15 18:10:13 所属栏目:百科 来源:网络整理
导读:从URI类型文件路径获取驱动器号的最简单的方法是什么? file:///D:/Directory/File.txt 我知道我可以做(路径这里是一个包含上面的文本的字符串) path = path.Replace(@"file:///",String.Empty);path = System.IO.Path.GetPathRoot(path); 但感觉有点笨拙.有
从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:
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读