prosseek
prosseek

Reputation: 190809

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

Upvotes: 212

Views: 207932

Answers (10)

Soatl
Soatl

Reputation: 10592

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.

Upvotes: 2

Christopher
Christopher

Reputation: 10627

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.

Upvotes: 2

Code_Beginner
Code_Beginner

Reputation: 79

You can try below code :

Path.GetFileName(userpath)

Upvotes: 0

Akshay
Akshay

Reputation: 447

This works perfectly fine with me :)

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

Upvotes: 3

Holf
Holf

Reputation: 6431

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

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

Upvotes: 7

codybartfast
codybartfast

Reputation: 7543

You could try:

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

Upvotes: 281

ProVega
ProVega

Reputation: 5914

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"

Upvotes: 24

Jakob Möllås
Jakob Möllås

Reputation: 4369

Well, to exactly answer your question title :-)

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

Upvotes: 12

Muad'Dib
Muad'Dib

Reputation: 29216

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

like so:

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

Upvotes: 10

SLaks
SLaks

Reputation: 887453

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

Upvotes: 145

Related Questions