Search code examples
c#directoryrootdrive

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


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

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


Solution

  • 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;