开发者

How can I get directory name of a path?

开发者 https://www.devze.com 2022-12-22 22:04 出处:网络
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\"

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)

开发者_C百科

Thanks!


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消