R
R
Roman Rakzin2017-07-03 23:50:11
ASP.NET
Roman Rakzin, 2017-07-03 23:50:11

How to get the path two folders above the running application?

There is a folder with an exe file, which, for example, I get with -Directory.GetCurrentDirectory().
How can I get a link to the two folders above this file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Arxont, 2017-07-04
@TwoRS

I think it's better to use the built-in language tools - in this case, the Directory.GetParent method called twice. Either cast the path to DirectoryInfo (or FileInfo) and look at the Parent property (called twice)

string dir = @"X:\Dir1\Dir2\Dir3\Dir4\Dir5\Dir6";

//1
Console.WriteLine(Directory.GetParent(
                                Directory.GetParent(dir).FullName).FullName);

 //2
DirectoryInfo dir2 = new DirectoryInfo(dir);
Console.WriteLine(dir2.Parent.Parent.FullName);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question