C# 获取C中的当前目录名(最后一部分)#

C# 获取C中的当前目录名(最后一部分)#,c#,string,filepath,C#,String,Filepath,我需要获取当前目录的最后一部分,例如从/Users/smcho/filegen\u from\u directory/AIRPassthrough,我需要获取AIRPassthrough 使用python,我可以通过以下代码获得它 import os.path path = "/Users/smcho/filegen_from_directory/AIRPassthrough" print os.path.split(path)[-1] 或 我怎样才能用C做同样的事情 补充 在回答者的帮助下

我需要获取当前目录的最后一部分,例如从
/Users/smcho/filegen\u from\u directory/AIRPassthrough
,我需要获取
AIRPassthrough

使用python,我可以通过以下代码获得它

import os.path

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

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

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

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

您正在寻找。
请注意,如果路径以
\
结尾,则此操作将不起作用

试试这个:

String newString = "";
String oldString = "/Users/smcho/filegen_from_directory/AIRPassthrough";

int indexOfLastSlash = oldString.LastIndexOf('/');

newString = oldString.Substring(indexOfLastSlash, oldString.Length);
代码可能已经关闭(我还没有测试过),但这个想法应该会奏效。

您可以尝试:

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

与其使用“/”来调用split,不如使用
路径。directoryseportorchar

像这样:

path.split(Path.DirectorySeparatorChar).Last() 
好吧,为了准确地回答你的问题,标题:-)


如果路径恰好包含正斜杠分隔符或反斜杠分隔符,则此选项有效。

这是一个稍有不同的答案,具体取决于您所拥有的内容。如果您有一个文件列表,并且需要获取文件所在的最后一个目录的名称,则可以执行以下操作:

string path = "/attachments/1828_clientid/2938_parentid/somefiles.docx";
string result = new DirectoryInfo(path).Parent.Name;

这将返回“2938\u parentid”

这对我来说非常好:)


您可以尝试以下代码:


Path.GetFileName(userpath)

您还可以使用Uri类

new Uri("file:///Users/smcho/filegen_from_directory/AIRPassthrough").Segments.Last()

如果您想获得其他部分,或者如果您想对一个网址做同样的事情,您可能更愿意使用这个类。

@anti:error;我试过了。字符串就是字符串。如果您不相信我,请将
Path.GetFileName(“/Users/smcho/filegen\u from\u directory/AIRPassthrough”)
粘贴到LINQPad中。@antisanity不,它将返回目录名,但可能与问题的内容不完全一致。@Light:它确实有效;试试看。
Path
类处理两个分隔符。要使用directory.GetDirectories检索的目录执行此操作,请使用:string directoryName=Path.GetFileName(directory.TrimEnd(Path.directoryseportorchar));如果你想要一个目录而不是一个文件名呢?我不喜欢使用这个解决方案的一点是,它假设字符串来自哪里。如果他们在Windows系统上读取*nix日志呢?然后错误的字符将被使用,您将得到整个路径,而不是预期的效果。就我的2美分。我同意你的看法。OP说的是“当前目录的最后一部分”,因此,这很好。有几种可能的重复:在python中,您应该执行
os.path.basename(path)
。我更喜欢这个答案,而不是选择的答案。(Ab)使用GetFileName在语义上是错误的,因为它是您试图检索的目录。另外,要使GetFileName具有确定性,意味着您必须考虑尾部可能出现正斜杠或反斜杠,并将其修剪掉。var dirName=new DirectoryInfo(Path.GetDirectoryName(pathWithFilename))。Name//better@kernowcode我相信这会返回“filegen\u from\u directory”,而不是“AIRPassthrough”“根据OP.@SimonBrangwin的请求,我认为Linux中的所有内容都是一个文件:).TrimEnd()的应用位置不正确。它应该应用于path,而不是path.GetFileName。看到@Fatlad的答案了吗
var lastPartOfCurrentDirectoryName = 
   Path.GetFileName(Environment.CurrentDirectory);
var lastFolderName = Path.GetFileName(
    path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
string path = "/attachments/1828_clientid/2938_parentid/somefiles.docx";
string result = new DirectoryInfo(path).Parent.Name;
Path.GetFileName(path.TrimEnd('\\')
new Uri("file:///Users/smcho/filegen_from_directory/AIRPassthrough").Segments.Last()