user3767769
user3767769

Reputation: 47

File Path and Root issues

So I have my the path to my website code as follows:

C:/folder1/folder2/folder3/my published website code from VS2012 - on my website I get an attachment and I want to save it to the following path C:/folder4

when I try the following code: file.SaveAs(Server.MapPath("../../folder4/") + filename); it says that I am going past the root. Can someone explain to me what is going on and if and how I can solve this issue?

Upvotes: 0

Views: 62

Answers (4)

Arve Systad
Arve Systad

Reputation: 5479

Server.MapPath(...) tries to return a physical ("real") directory for the virtual or relative path you give it. And since a virtual directory can't be located "over" the root in that sense, what you're trying to do makes no sense. You can go from domain.com/somefolder to domain.com/, but you can't really go any farther back.

You could instead use Environment.CurrentDirectoryas the starting point to find your folder, and apart from that just use SaveAs(..) as you're already doing.

Upvotes: 0

NSjonas
NSjonas

Reputation: 12032

Server.MapPath() is used to get the path in relation to the server root. Since your trying to save it outside the server virtual directory, you could probably just hardcode the file.

file.SaveAs(@"C:/folder4/" + filename);

It might not work depending on your IIS worker pool permissions.

Upvotes: 1

Rune
Rune

Reputation: 8380

As per the documentation for HttpServerUtility.MapPath:

you cannot specify a path outside of the Web application

which is exactly what you are trying to do. If you interpret "the root" to be the root folder of your application, that is even what the error message is telling you.

Either

  1. use an absolute path or
  2. store your data beneath the application folder
  3. use MapPath("~/") to get the current directory and build a relative path from that (in essence, you just move the "../.." outside the call to MapPath)

I would probably recommend going with 2. as it will give less headaches wrt. permissions and multiple sites hosted on the same server.

Upvotes: 0

user3842306
user3842306

Reputation: 56

file.SaveAs(Server.MapPath("folder4/") + filename); Because I cannot see your folders structure I would reccomend setting a breakpoint after Server.MapPath() to see the full URI Path to determin your next steps since it says you are past root you may have one to many "../" before your string.

Upvotes: 0

Related Questions