获得在C#中(的最后一部分)当前目​​录名 [英] Get the (last part of) current directory name in C#

查看:170
本文介绍了获得在C#中(的最后一部分)当前目​​录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从获取当前目录的最后一部分,例如/用户/ smcho / filegen_from_directory / AIRPassthrough ,我需要获得 AIRPassthrough



与Python,我可以用这个代码得到它。

 进口os.path中

路径=/用户/ smcho / filegen_from_directory / AIRPassthrough
打印os.path.split这样的(路径)[ - 1]

 打印os.path中。基本名(路径)

我如何做同样的事情用C#?



ADDED



从回答者的帮助下,我找到了我需要的东西。

 使用System.Linq的; 
串FULLPATH = Path.GetFullPath(完整路径).TrimEnd(Path.DirectorySeparatorChar);
串PROJECTNAME = fullPath.Split(Path.DirectorySeparatorChar)。去年();

 字符串FULLPATH = Path.GetFullPath(完整路径).TrimEnd(Path.DirectorySeparatorChar); 
串PROJECTNAME = Path.GetFileName(完整路径);


解决方案

您正在寻找的 Path.GetFileName 结果
请注意,如果路径在 \ 结束,这将不起作用。


I need to get the last part of current directory, for example from /Users/smcho/filegen_from_directory/AIRPassthrough, I need to get AIRPassthrough.

With python, I can get it with this code.

import os.path

path = "/Users/smcho/filegen_from_directory/AIRPassthrough"
print os.path.split(path)[-1]

Or

print os.path.basename(path)

How can I do the same thing with C#?

ADDED

With the help from the answerers, I found what I needed.

using System.Linq;
string fullPath = Path.GetFullPath(fullPath).TrimEnd(Path.DirectorySeparatorChar);
string projectName  = fullPath.Split(Path.DirectorySeparatorChar).Last();

or

string fullPath = Path.GetFullPath(fullPath).TrimEnd(Path.DirectorySeparatorChar);
string projectName = Path.GetFileName(fullPath);

解决方案

You're looking for Path.GetFileName.
Note that this won't work if the path ends in a \.

这篇关于获得在C#中(的最后一部分)当前目​​录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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