35

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

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

5 Answers5

36

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;

Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
11

Try this:

if (Path.GetPathRoot(location) == location) {...}
Peter
  • 37,042
  • 39
  • 142
  • 198
Brandon
  • 1,412
  • 11
  • 15
  • he already has the path, he wants to see if it's the root, not get the root. – Dustin Davis Feb 18 '11 at 23:24
  • 5
    @Titan: If getting the root returns the same string, then it's a root. It may not be the best approach, but it is valid. – Ben Voigt Feb 19 '11 at 00:16
  • @Ben thats true but it's far less efficient. It not only requires working with 2 strings but comparing them too. – Dustin Davis Feb 19 '11 at 05:13
  • 5
    @DustinDavis far less efficient then what? if you allocate a `DirectoryInfo` you now have to GC a `DirectoryInfo` instead of a string, `GetVolumeNameForVolumeMountPoint` uses interop and will also have overhead, `Directory.GetLogicalDrives()` returns a string array... – Peter Jun 02 '15 at 07:03
5

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.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • My program is in C# and GetVolumeNameForVolumeMountPoint is a native C function. Anyway DirectoryInfo.Parent seems to work perfectly. Thanks for the tip though. –  Feb 24 '11 at 03:20
  • 1
    It works perfectly unless you ask it about a mount point. In which case `DirectoryInfo.Parent` will tell you it is a subdirectory when in fact it is the root directory of another partition. There is no managed function to test whether a particular directory is the root of its partition, which is why I suggested a native Win32 function. – Ben Voigt Feb 24 '11 at 05:03
3

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!

cweston
  • 11,297
  • 19
  • 82
  • 107
  • Is it necessary to create 2 objects? How about "return path == Path.GetPathRoot(path)" – Darrel Lee Jul 18 '16 at 11:33
  • @DarrelLee, while I agree that `Path.GetPathRoot()` can be the more convenient option in some cases, it does not find the root of relative paths. https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getpathroot?view=netframework-4.7 Also, since we're using a string comparison we'll want to make sure both sides get the same formatting. – Kyle Delaney Jul 17 '17 at 13:24
  • On that note, even `new DirectoryInfo(path).Root.FullName` isn't sure to be consistent because it could return an upper or lower case version of the drive letter. Better do a string conversion as well. – Kyle Delaney Jul 17 '17 at 13:45
1

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.

  • Not all partitions have their own drive letter. http://technet.microsoft.com/en-us/library/cc938934.aspx – Ben Voigt Feb 24 '11 at 05:05
  • `Directory.GetLogicalDrives` returns a `string[]`. `System.Array` implements `System.Collections.IList`, but it provides an explicit interface implementation for Contains: ((.NET 3.5+: https://msdn.microsoft.com/en-us/library/bb336401(v=vs.110).aspx )) ((.NET 2, 3: https://msdn.microsoft.com/en-US/library/system.array.system.collections.ilist.contains(v=vs.85).aspx )) ((.NET 1.1: https://msdn.microsoft.com/en-us/library/aa310885(v=vs.71).aspx )) The array must be cast to an `IList` before calling Contains. – monkey0506 Jan 21 '15 at 02:47