Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.MapPath to go two folder back from root

Tags:

c#

asp.net

iis

Here is how I do it:

HttpContext.Current.Server.MapPath(@"~\~\~\Content\")

HI know that '.' is for the root of the project, but how to go a few folders back?

like image 851
petko_stankoski Avatar asked Mar 15 '12 18:03

petko_stankoski


2 Answers

If you really need the grandparent path, you can get it from the root path using Path.GetDirectoryName():

string root = Server.MapPath("~");
string parent = Path.GetDirectoryName(root);
string grandParent = Path.GetDirectoryName(parent);

But your web app very likely won't have permission to read or write there - I'm not sure what you're going to do with it.

like image 112
Rup Avatar answered Sep 19 '22 04:09

Rup


You can use Parent.Parent.FullName

  string grandParent  = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/")).Parent.Parent.FullName;
like image 33
David Smit Avatar answered Sep 20 '22 04:09

David Smit