tags:

views:

70

answers:

3

I tried using Path.GetDirectoryName() but it doesn't work.

What I'm trying to get is from /home/nubela/test/some_folder , I wanna get "some_folder"

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

Thanks!

+2  A: 

Use Path.GetFileName instead? These functions work just on the string you provide and don't care if it's a directory or a file path.

Pent Ploompuu
I sure hope so, we're always saying to use the Path functions for portability.
Henk Holterman
That is the proper way. The method name is misleading, but what it does is just get the final part of the path as nubela wants. Beware of the special caveat, if you give it a path like "C:\temp\" it will return an empty string as it just gets what exists after the last path separator, which is at the end of the string in this case. See http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx
Monoman
+1  A: 

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

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

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

Andrew Hare
A: 

My first idea would be to use System.IO.Path.GetDirectoryName, too. But you can try a regular expression to get the final substring of your string. Here is an answer in StackOverflow, using regular expressiones, that answers this.

Javier Morillo