Path from previous folder

Asked

Viewed 1,070 times

10

How can I get the path from the previous folder?
For example I have this path : C:/Pasta1/Pasta2
And I wanted to get in the way of folder 1 C:/Pasta1
How can I do that?

2 answers

12


I believe you desire the GetParent().

using static System.Console;
using System.IO;

public class Program {
    public static void Main() => WriteLine(Directory.GetParent("/Pasta1/Pasta2"));
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

You just have to be careful if you end up with a bar because it will make the chain nothing, and the last one listed is the father. If such a situation can occur it is best to treat it properly.

6

The following is an example of the use of GetParent() class System.IO.Directory

var caminho1 = Directory.GetParent("C:\\Pasta1\\Pasta2");
//caminho1 = "C:\Pasta1"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.