C#如何知道给定路径是否代表根驱动器? [英] C# How to know if a given path represents a root drive?

查看:144
本文介绍了C#如何知道给定路径是否代表根驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何知道给定的目录是否是根驱动器?

How can I know if a given directory is a root drive?

(除了检查其路径是否等于A:,B:, C:等)

(aside from checking if its path equals to "A:", "B:", "C:", etc.)

推荐答案

检查DirectoryInfo.Parent是否为空

Check if DirectoryInfo.Parent is null or not

DirectoryInfo d = new DirectoryInfo("");
if(d.Parent == null) { IsRoot = true; }

您还可以使用DirectoryInfo.Root获取根;

you can also get the root by using DirectoryInfo.Root;

这篇关于C#如何知道给定路径是否代表根驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆