Reputation: 26538
I have a file path as
D:\Accurev\PPF6-AvailableToUse_Test_4_4\eBizSol_App\Source\xyz.txt
If I do
Path.GetDirectoryName(fileName)
I get
D:\Accurev\PPF6-AvailableToUse_Test_4_4\eBizSol_App\Source
But I want to get only the root directory i.e. "D:\\"
How can I get it?
N.B.~ Is it possible without string splitting?
Upvotes: 1
Views: 6945
Reputation: 9458
You can use Path.GetPathRoot
Method for this.
So, you can simply have string root = Path.GetPathRoot(fullFileName);
But, this method does not verify that the path or file name exists.
Possible patterns for the string returned by this method are on MSDN as follows:
"/"
(path specified an absolute path on the current drive)."X:"
(path specified a relative path on a drive, where X represents a drive or volume letter)."X:/"
(path specified an absolute path on a given drive)."\\ComputerName\SharedFolder"
(a UNC path).Upvotes: 1
Reputation: 19588
String pathname= @"D:\Accurev\PPF6-AvailableToUse_Test_4_4\eBizSol_App\Source\xyz.txt";
string root = Path.GetPathRoot(pathname);
Upvotes: 3
Reputation: 4703
You're in luck, there're several ways to do the same thing. Here're two of them:
Path.GetRootPath as other answers already shown
DirectoryInfo.Root property of FileInfo
class:
var fileName=
@"D:\Accurev\PPF6-AvailableToUse_Test_4_4\eBizSol_App\Source\xyz.txt";
var file=new FileInfo(fileName);
var root=file.Directory.Root;
Upvotes: 2
Reputation: 223392
Use Path.GetPathRoot method provided by the framework
Gets the root directory information of the specified path
For your case you can use:
string rootPath = Path.GetPathRoot(filename);
Upvotes: 6