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
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.
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.
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.
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.
string dir = new DirectoryInfo(fullpath).Name;
try this
string s =new System.IO.DirectoryInfo(@"C:\MyDir1\MyDir2\MyDir3").Name;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With