Click here to Skip to main content
15,914,109 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

I have the following code to check for path, if not exist create. But it keeps returning with an error.

Code:

C#
string path1 = "~/uploads/" + getpono + "/" + getmrr + "/";

        if (!System.IO.Directory.Exists(path1))
        {
            System.IO.Directory.CreateDirectory(path1);
        }



Error:

Access to the path '~/uploads/147302/BFM-400523-1001/' is denied.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.UnauthorizedAccessException: Access to the path '~/uploads/147302/BFM-400523-1001/' is denied. 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.


I was able to create the folder before, but all of sudden i can not create anymore. I have made sure the folder as permissions for IUSER_MACHINENAME but still nothing.

Any idea?
Posted

You cannot directly use such paths. The paths in the form "~/*..." are the paths relative to the root directory configured for your site. But the .NET API (say, System.IO) does not "know" this path. You can find it from your HTTP request via MapPath:
HttpServerUtility.MapPath Method (String) (System.Web)[^].

See also: HttpServerUtility.MapPath Method (String) (System.Web)[^].

—SA
 
Share this answer
 
Use below code :
C#
string path1 = Server.MapPath("~/uploads/" + getpono + "/" + getmrr + "/");

Try setting the access permissions to "Full control" for the .Net user from where you are reading/saving the files.

Hopefully it will work ...!
 
Share this answer
 
The error is accurate. I suggest going into IIS and setting the identity of the application pool to an account that has permissions to the file system.
 
Share this answer
 
~ symbol is generally used to work with URLs, Specify fully qualified path on server like 'C:\\YOUR_PATH\\uploads'.
(Keeping this path value in config file is recommended instead of baking it directly in code)


Make sure you provide proper permissions to user account configured in IIS App Pool(Advance settings) for reading/writing to folder/path.

~ symbol may try to work on the path where the worker process is scoped in the Operating system. Better use your own full path for better control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900