8

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?

Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
petko_stankoski
  • 9,682
  • 37
  • 116
  • 215

5 Answers5

14

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.

Rup
  • 31,518
  • 9
  • 83
  • 102
  • 2
    root, parent and grandParent all return the same path. Why? – petko_stankoski Mar 15 '12 at 18:22
  • 1
    Really? I don't have an ASP.NET site to hand to test this in, but I tried an example in LinqPad starting from c:\inetpub\wwwroot\myapp and it worked OK. I'd be surprised if Path was set up to restrict you - after all it's just processing a string. So I haven't tested this in the exact environment but I am surprised if it doesn't just work. (I realise now my code is assuming root doesn't have a trailing slash - you might need another Path.GetDirectoryName to get rid of that if one exists.) – Rup Mar 15 '12 at 18:27
4

You can use Parent.Parent.FullName

  string grandParent  = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/")).Parent.Parent.FullName;
David Smit
  • 709
  • 1
  • 11
  • 30
4

Start with the root of your site with ~ and specify the full path: ~/Archive/Content.

You can't go back above site root because of security restrictions, see also this article from other solutions.

IIS purposefully prevents serving up content directly that is outside of the site path. However, you can create a virtual directory in IIS and have ISAPI Rewrite point to that. For example, create a virtual directory called /staticfiles that points to c:\test\data\static-files. As far as IIS is concerned, that's directly off of the root of the site in a folder called /staticfiles.

Adriano Repetti
  • 60,141
  • 17
  • 127
  • 190
1

Since you are using MapPath you are getting returned a physical path(\) from a virtual path(/).

Creating a DirectoryInfo object or using a Path utility method starting from your child app root won't necessarily give you what you expect unless your virtual parent and virtual grandparent have the same hierarchy as your physical directory structure.

My apps are not physically nested to match the Url depth. This could also be the case if a virtual directory is involved.

Assuming a grandparent app is two virtual folders up this would get you the physical path...

    string physicalGrandparentPath = HttpContext.Current.Server.MapPath("~/../../");

Using this would keep you safe from any virtual directory shuffle games going on in the IIS setup.

I used this to see how far I could go up. I did not get an HttpException until I tried to go above wwwroot.

eric1825
  • 405
  • 2
  • 12
0

Easiest way, you can still use the following:

string MyFolderName = Server.MapPath("~/AliasName/");

Add a Virtual Directory to your application. Here's how:

  • In Visual Studio under your root folder right click:
  • Add > New Virtual Directory
  • Alias name: AliasName
  • Folder: click on > Browse...
  • Navigate to the folder you need and done.

That's it, now you have access!

Jacman
  • 1,162
  • 2
  • 16
  • 27