我需要获取当前目录的最后一部分,例如从/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/