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

C#StringFilepath

C# Problem Overview


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

C# Solutions


Solution 1 - C#

You could try:

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

Solution 2 - C#

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

Solution 3 - C#

This is a slightly different answer, depending on what you have. If you have a list of files and need to get the name of the last directory that the file is in you can do this:

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

This will return "2938_parentid"

Solution 4 - C#

Well, to exactly answer your question title :-)

var lastPartOfCurrentDirectoryName = 
   Path.GetFileName(Environment.CurrentDirectory);

Solution 5 - C#

rather then using the '/' for the call to split, better to use the Path.DirectorySeparatorChar :

like so:

path.split(Path.DirectorySeparatorChar).Last() 

Solution 6 - C#

var lastFolderName = Path.GetFileName(
    path.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));

This works if the path happens to contain forward slash separators or backslash separators.

Solution 7 - C#

Try this:

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

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

newString = oldString.Substring(indexOfLastSlash, oldString.Length);

Code may be off (I haven't tested it) but the idea should work.

Solution 8 - C#

This works perfectly fine with me :)

Path.GetFileName(path.TrimEnd('\\')

Solution 9 - C#

You can also use the Uri class.

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

You may prefer to use this class if you want to get some other segment, or if you want to do the same thing with a web address.

Solution 10 - C#

You can try below code :

Path.GetFileName(userpath)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - C#codybartfastView Answer on Stackoverflow
Solution 2 - C#SLaksView Answer on Stackoverflow
Solution 3 - C#ProVegaView Answer on Stackoverflow
Solution 4 - C#Jakob MöllåsView Answer on Stackoverflow
Solution 5 - C#Muad'DibView Answer on Stackoverflow
Solution 6 - C#HolfView Answer on Stackoverflow
Solution 7 - C#SoatlView Answer on Stackoverflow
Solution 8 - C#AkshayView Answer on Stackoverflow
Solution 9 - C#ChrisView Answer on Stackoverflow
Solution 10 - C#Code_BeginnerView Answer on Stackoverflow