C# 如何知道给定路径是否代表根驱动器?
我如何知道给定目录是否是根驱动器?
(除了检查其路径是否等于“A:”、“B:”、“C:”等)
How can I know if a given directory is a root drive?
(aside from checking if its path equals to "A:", "B:", "C:", etc.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查DirectoryInfo.Parent是否为空,
您也可以使用DirectoryInfo.Root获取根;
Check if DirectoryInfo.Parent is null or not
you can also get the root by using DirectoryInfo.Root;
尝试这个:
Try this:
这比检查 Parent 属性要复杂得多。
确定目录是否是已安装的文件夹
一种方法是查看
GetVolumeNameForVolumeMountPoint
成功。当然,这不适用于网络路径,可能无法远程确定网络驱动器是否代表分区的根目录。
It's much more complicated than checking the Parent property.
Determining Whether a Directory Is a Mounted Folder
One approach would be to see if
GetVolumeNameForVolumeMountPoint
succeeds.Of course that won't work for network path, determining if a network drive represents the root directory of a partition may not be possible remotely.
另外,这是我发现的另一种方法:
如果此函数返回 true,则意味着给定路径代表根驱动器!
Also Here's another way I found:
if this function returns true, then it means that given path represents a root drive!
这是我发现的另一种方法:
这个方法实际上检查给定路径是否代表当前系统的逻辑驱动器之一。
Here's another way I found:
This one actually checks if the given path represents one of the current system's logical drives.