Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net, built in way to get directory name from path?

Tags:

.net

Given the following directory:

string fullpath = "C:\MyDir1\MyDir2\MyDir3";

I would like to return "MyDir3" - this being the directory name (not full path, of a directory) , I know I can do this using string manipulation, but is there an easy (built in way) to achieve this using framework classes?

Thanks

like image 499
JL. Avatar asked Mar 03 '10 09:03

JL.


People also ask

How do I get the filename from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null.

How do I find the path to a folder in Visual Studio?

Ctrl+Click or Double Right Click to Open Containing Folder, Right click to Copy Full Path. This lightweight extension lets you display the full path of the file at bottom of Visual Studio's Editor.

Does a file path include the name?

Paths include the root, the filename, or both. That is, paths can be formed by adding either the root, filename, or both, to a directory.

What is directory in path?

A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory.


2 Answers

string dir = new DirectoryInfo(fullpath).Name;
like image 87
Thomas Levesque Avatar answered Nov 16 '22 04:11

Thomas Levesque


try this

string s =new  System.IO.DirectoryInfo(@"C:\MyDir1\MyDir2\MyDir3").Name;
like image 25
hallie Avatar answered Nov 16 '22 03:11

hallie