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.
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);
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.
(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.)
Like this:
Path.GetFileName(Path.GetDirectoryName(something))
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