开发者

C# How to know if a given path represents a root drive?

开发者 https://www.devze.com 2023-02-12 13:33 出处:网络
How can I know if 开发者_StackOverflowa given directory is a root drive? (aside from checking if its path equals to \"A:\", \"B:\", \"C:\", etc.)Check if DirectoryInfo.Parent is null or not

How can I know if 开发者_StackOverflowa given directory is a root drive?

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


Check if DirectoryInfo.Parent is null or not

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

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


Try this:

if (Path.GetPathRoot(location) == location) {...}


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.


Also Here's another way I found:

 public static bool IsLogicalDrive(string path)
 {
     return (new DirectoryInfo(path).FullName == new DirectoryInfo(path).Root.FullName);
 }

if this function returns true, then it means that given path represents a root drive!


Here's another way I found:

public static bool IsLogicalDrive(string path)
{
    return Directory.GetLogicalDrives().Contains(path);
}

This one actually checks if the given path represents one of the current system's logical drives.

0

精彩评论

暂无评论...
验证码 换一张
取 消