IIS - Access Denied writing file to relative path in ASP.NET

Asked By Henry Taylor on 10-Nov-14 11:36 PM
I am trying to write to a directory in a path something like this:

~/Folder_on_the_root/TARGET/

TARGET exists. I am trying to create directory through code and then write a file there.

I keep getting:

Access to the path '~\Membership\docs\Contract_Output\9999999' is denied.

I have set the Contract_Ouput directory wide open but it still won't let me write to it.

Doh!

What could be going on?????

Thanks.
Robbe Morris replied to Henry Taylor on 10-Nov-14 06:11 PM
var physicalPath = Server.MapPath("~/Folder_on_the_root/TARGET/");

You have to convert the relative path to a full physical path in order to read/write files.

If you intend to write the file in production, you'll have to give the windows account your IIS app pool is running under write access to the desired folder.  It would not have it by default.