1

I am able to get the current path through:

Request.PhysicalApplicationPath;

For example, it is C:\consoleapp\capp\files

I want to go up one folder to : C:\consoleapp\capp\

and then to another folder C:\consoleapp\capp\images

How do I do that?

nirilo
  • 41
  • 1
  • 6
  • Check [Path](https://learn.microsoft.com/en-us/dotnet/api/system.io.path?view=netframework-4.7.2) static methods. – Sinatr Jan 25 '19 at 15:39

3 Answers3

2

Following should help you to Get to the Parent Directory and then create the new Path based on it.

var path = @"C:\consoleapp\capp\files"; // or in your case Request.PhysicalApplicationPath
var parent = Directory.GetParent(path).FullName;
var newPath = Path.Combine(parent,"Images");

You can read more details on Directory.GetParent() here. Path.Combine allows to you combine multiple strings to path. You can read more on Path.Combine here

Output

parent = C:\consoleapp\capp
newPath = C:\consoleapp\capp\Images
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
2

You should use System.IO
You can use DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory()) and then just use di.Parent

Nekeniehl
  • 1,633
  • 18
  • 35
0

From How to navigate a few folders up?

string path = @"C:\Folder1\Folder2\Folder3\Folder4";
string newPath = Path.GetFullPath(Path.Combine(path, @"..\..\"));

After you navigated up you can attach the new folder again

Benyom
  • 75
  • 7
  • Why you have deleted the [concat 2 strings question](https://stackoverflow.com/questions/54570681/concat-2-strings-return-null-if-one-is-empty). It was interesting once it was clear.I have edited it to make it clearer. Undelete it and i can answer – Tim Schmelter Feb 07 '19 at 10:12