prosseek prosseek - 2 months ago 6
C# Question

Get the (last part of) current directory name in C#

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

Answer

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

Comments