0

Debugging locally on Windows 10 machine, VS2015.

I need to open a file with Read access only. That file exists in a sub-folder of the web application.

This throws an exception:

System.UnauthorizedAccessException: Access to the path 'F:\webroot\subfolder' is denied.

I have tried adding Everyone with Full Permissions, adding every user with Full Permissions, turning on security policy auditing and checking the Security log to uncover the requesting user but nothing appears in there as if it is not actually a security error, a different folder name, and everything else I can find online including adding the ASPNET user - there is no such user to add.

The calculated path is the correct physical disk path. The path is inside the webroot.

string FilePath = HttpContext.Current.Server.MapPath("\\Upload"); string PathToFile = FilePath + "\\" + idFileName; Stream fs = File.Open(FilePath, FileMode.Open, FileAccess.Read); 

The final line of code throws the exception.

ASP.NET web page output: Access to the path 'F:\webrootname\Upload' is denied.

Application Event Log:

Event code: 4011 Event message: An unhandled access exception has occurred.

2
  • 1
    This can happen if you try to open a "file" when the path actually points to a directory. Based on the exception message you posted, it looks like this might be the issue. Commented May 29, 2019 at 16:18
  • You aren't using PathToFile anywhere... Commented May 29, 2019 at 16:31

1 Answer 1

3

Maybe it's because you're calling FileOpen with FilePath instead of PathToFile that you calculated, so :

Stream fs = File.Open(PathToFile, FileMode.Open, FileAccess.Read); 

Also, you can go further and test if the file exists before opening it :

string FilePath = HttpContext.Current.Server.MapPath("\\Upload"); string PathToFile = FilePath + "\\" + idFileName; if(System.IO.File.Exists(PathToFile)) { Stream fs = File.Open(PathToFile, FileMode.Open, FileAccess.Read); } else { // use whatever logger to trace your application log.Error("The file : + PathToFile + " does not exist"); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.