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.
views:
85answers:
6Snazzy, I didn't know you could tackle the problem like this. +1
AndyPerfect
2010-10-11 23:42:36
Great, this is the most straightforward. Thanx!
WinnerWinnerChickenDinner
2010-10-12 16:05:57
+1
A:
Have a look at this answer; C# How do I extract each folder name from a path? and then just go for the last element in the array.
Dave Anderson
2010-10-11 23:37:28
+2
A:
Try this:
var directoryFullPath = Path.GetDirectoryName(@"C:\My Documents\Images\Image1.png");
var directoryName = Path.GetFileName(directoryFullPath); \\ Images
Leniel Macaferi
2010-10-11 23:38:11
+2
A:
Use System.IO.FileInfo
.
string fl = "C:\My Documents\Images\Image1.png";
System.IO.FileInfo fi = new System.IO.FileInfo(fl);
string owningDirectory = fi.Directory.Name;
code4life
2010-10-11 23:38:49
+1
A:
The following method will extract all the directory names and file name
Dim path As String = "C:\My Documents\Images\Image1.png"
Dim list As String() = path.Split("\")
Console.WriteLine(list.ElementAt(list.Count - 2))
AndyPerfect
2010-10-11 23:39:15
well, the title hints at the possibility of getting multiple folder names - wasn't sure if the asker may have wanted more than just the one parent directory, so why not?
AndyPerfect
2010-10-11 23:53:48
+3
A:
Create an instance of
System.IO.FileInfo f1 = new FileInfo("filepath");
DirectoryInfo dir=f1.Directory;
string dirName = dir.Name;
string fullDirPath = dir.FullName;
AsifQadri
2010-10-12 10:42:15