Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a filepath say "C:\hello\hi\dotnet\abc.txt". I just want the directories from the path.

Expected Output :
hello
hi
dotnet

I have used Path.DirectorySeparatorChar but it doesn't work.So help me out to solve this problem.

Thanks
Stone Cold
Posted

You may also use the System.IO.FileInfo[^] class. You can then access the directory part of the path using FileInfo.DirectoryName [^].

You can access the FileInfo.Directory[^] which returns a DriveInfo object - use the DirectoryInfo.Name[^] to get the name of the directory, then use DirectoryInfo.Parent[^] to access the parent directory. Repeat until you you have reached the root directory of the drive - i.e. Parent returns null.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Simon Bang Terkildsen 14-Oct-11 18:31pm    
my 5
Espen Harlinn 15-Oct-11 4:13am    
Thank you, Simon!
Use Split and exclude the 0 and the last entry
C#
string[] dirs = pathname.Split('\\');
//dirs[1] will contain hello etc 
 
Share this answer
 
Comments
Simon Bang Terkildsen 14-Oct-11 18:32pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900