You can use the Path and Directory classes:
DirectoryInfo parentDir = Directory.GetParent(Path.GetDirectoryName(path)); string parent = parentDir.FullName;
Note that you will get a different result if the path does not end with char \ directory separator. Then images will be understood as a file name, not as a directory.
You can also use a subsequent call to Path.GetDirectoryName
string parent = Path.GetDirectoryName(Path.GetDirectoryName(path));
This behavior is documented here :
Since the returned path does not include DirectorySeparatorChar or AltDirectorySeparatorChar, passing the returned path back to the GetDirectoryName method will truncate one level folder for the next call in the result line. For example, passing the path "C: \ Directory \ SubDirectory \ test.txt" to the GetDirectoryName method will return "C: \ Directory \ SubDirectory". Passing this string "C: \ Directory \ SubDirectory" to GetDirectoryName will result in "C: \ Directory".
source share