Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the root directory without full path in C#

Tags:

c#

Is their any way to find root directory without knowing the full path.

For example, I'm having Folder\subfolder I want to find the root directory like c:\, d:\, e:\ in which that subfolder presents

I had tried by using DirectoryInfo("path").Root.Name, Path.GetPathRoot(path) but it doesn't works.

like image 815
user2477462 Avatar asked Sep 12 '13 12:09

user2477462


2 Answers

You first need to the full path and then the root of it:

 string path = ...
 string root = Directory.GetDirectoryRoot(Path.GetFullPath(path));
like image 169
joe Avatar answered Sep 28 '22 05:09

joe


Try:

string result = Path.GetPathRoot(Path.GetFullPath("MyPath"));

This uses the current working directory (which is the only way that you can convert a relative path to a full path - the relative path is always relative to the current working directory).

like image 45
Matthew Watson Avatar answered Sep 28 '22 04:09

Matthew Watson