Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Folder Name(s) from Path

Tags:

c#

.net

vb.net

If I have a file path like "C:\My Documents\Images\Image1.png", how can I get the parent folder name of the "Image1.png" file? In this case, "Images", but that's just a sample. I've looked through System.IO.Path and there doesn't seem to be anything there. Maybe I'm overlooking it, but I have no idea where it would be.

like image 369
Stan Avatar asked Oct 11 '10 23:10

Stan


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. Syntax: public static string GetFileName (string path);

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 a folder's path?

(Alternate definition: A directory is a folder.) Path: This term is descriptive in that it represents a type of "road map" to a specific file or directory. (Alternate definition: A path is a list, beginning with a drive letter, that tells which folders to open so that you can find a file or another folder.)


1 Answers

Like this:

Path.GetFileName(Path.GetDirectoryName(something))
like image 142
SLaks Avatar answered Sep 22 '22 01:09

SLaks