如何获取路径的目录名称? [英] How can I get directory name of a path?

查看:84
本文介绍了如何获取路径的目录名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Path.GetDirectoryName(),但是它不起作用.

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

我想从/home/nubela/test/some_folder获得,我想获得"some_folder"

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

我该怎么做?该方法适用于Windows/Linux(Mono)

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

谢谢!

推荐答案

如果路径已经是字符串,则可以使用此方法提取最低级别的目录:

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);

由于此代码使用Path.DirectorySeparatorChar,因此它与平台无关.

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

这篇关于如何获取路径的目录名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆