How can I get the path directory name?

I tried using Path.GetDirectoryName (), but it does not work.

What I'm trying to get from / home / nubela / test / some _folder, I want to get "some_folder"

How can i do this? This method should work for both Windows / Linux (Mono)

Thanks!

0
source share
3 answers

If you have a path as a string, you can use this method to extract the lower level directory:

String dir = yourPath.Substring( yourPath.LastIndexOf(Path.DirectorySeparatorChar) + 1); 

Since this code uses Path.DirectorySeparatorChar , it is platform independent.

+1
source

Use Path.GetFileName instead? These functions only work on the line you provide, and do not care whether it is a directory or a file path.

+6
source

My first idea would also be to use System.IO.Path.GetDirectoryName. But you can try regex to get the final substring of your string. fooobar.com/questions/296758 / ... using regular expressions that answer this.

0
source

All Articles