How to get second to last directory in path line in C #

For instance,

string path = @"C:\User\Desktop\Drop\images\"; 

I need to get only @"C:\User\Desktop\Drop\

Is there an easy way to do this?

+4
source share
7 answers

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".

+8
source
 var parent = ""; If(path.EndsWith(System.IO.Path.DirectorySeparatorChar) || path.EndsWith(System.IO.Path.AltDirectorySeparatorChar)) { parent = Path.GetDirectoryName(Path.GetDirectoryName(path)); parent = Directory.GetParent(Path.GetDirectoryName(path)).FullName; } else parent = Path.GetDirectoryName(path); 

As I commented on GetDirectoryName, it collapses itself, it returns the path without tralling slash - it allows you to get the following directory. Directory.GetParent is also used for then clouse.

+1
source

This will return "C: \ User \ Desktop \ Drop \", for example. all but the last subdir

 string path = @"C:\User\Desktop\Drop\images"; string sub = path.Substring(0, path.LastIndexOf(@"\") + 1); 

Another solution if you have a trailing slash:

 string path = @"C:\User\Desktop\Drop\images\"; var splitedPath = path.Split('\\'); var output = String.Join(@"\", splitedPath.Take(splitedPath.Length - 2)); 
0
source

The example at the bottom of the page will probably help: http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname(v=vs.110).aspx

0
source
 using System; namespace Programs { public class Program { public static void Main(string[] args) { string inputText = @"C:\User\Desktop\Drop\images\"; Console.WriteLine(inputText.Substring(0, 21)); } } } 

Output:

C: \ User \ Desktop \ Drop \

0
source

There may be an easy way to do this using the File or Path classes, but you can also solve it by doing something like this (Note: not tested):

 string fullPath = "C:\User\Desktop\Drop\images\"; string[] allDirs = fullPath.split(System.IO.Path.PathSeparator); string lastDir = allDirs[(allDirs.length - 1)]; string secondToLastDir= allDirs[(allDirs.length - 2)]; // etc... 
0
source

You can go back how many levels :)

 for (int i = 0; i < 3; i++) path = Directory.GetParent(path).ToString(); 
0
source

All Articles