This post has been republished via RSS; it originally appeared at: IIS Support Blog articles.
If the file you are trying to access is not available or you don’t have permissions to access it, your web application (or SharePoint web part in my case) may throw System.UnauthorizedAccessException error. Full error message:
An exception of type “System.UnauthorizedAccessException” occurred in mscorlib.dll but was not handled in user code. Additional information: Access to the path ‘\networkshare\filename.ext’ is denied.”
Root Cause
In the case I worked on, the code line that was trying to open the file was using File.OpenRead
:
File.OpenRead
tries to open a file with the name in the local machine, not in the server. The file is most likely in the server, not in the local (client) machines. Therefore, it throws an error even if you assigned perrmissions to the folder.
Solution
Use InputStream instead of File.OpenRead. InputStream will make sure to use the file on the server side.
If you want to save the content of stream to SharePoint, you can decode it by using the method below: