c# - 在 C# 中获取(最后一部分)当前目录名称

标签 c# string filepath

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

使用python,我可以用这段代码得到它。

import os.path

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

或者

print os.path.basename(path)

我怎样才能用 C# 做同样的事情?

已添加

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

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

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

最佳答案

你可以试试:

var path = @"/Users/smcho/filegen_from_directory/AIRPassthrough/";
var dirName = new DirectoryInfo(path).Name;

关于c# - 在 C# 中获取(最后一部分)当前目录名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018293/

相关文章:

c# - 将文本文件写入子文件夹

c# - 具有继承实体的 MVC 5 脚手架代码生成导致 System.Core 已导入

符合标准的字符串是否可以超过 SIZE_MAX 个字符?

c - 为什么 GCC 编译器允许我们声明零长度字符串(直接)和负长度字符串(间接)?

java - java中如何确定启动应用程序的文件路径

python - 在python中将文件路径存储为变量

c# - 使用 Microsoft.ML 和 DetectIidSpike 了解尖峰

c# - 使用 MySqlTransaction 回滚

c# - 打开和打印 PDF 文件

c - 编写一个仅执行三个有效命令的 C 程序