Home
%3CLINGO-SUB%20id%3D%22lingo-sub-784516%22%20slang%3D%22en-US%22%3ESystem.UnauthorizedAccessException%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-784516%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20the%20file%20you%20are%20trying%20to%20access%20is%20not%20available%20or%20you%20don%E2%80%99t%20have%20permissions%20to%20access%20it%2C%20your%20web%20application%20(or%20SharePoint%20web%20part%20in%20my%20case)%20may%20throw%20System.UnauthorizedAccessException%20error.%20Full%20error%20message%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3EAn%20exception%20of%20type%20%E2%80%9CSystem.UnauthorizedAccessException%E2%80%9D%20occurred%20in%20mscorlib.dll%20but%20was%20not%20handled%20in%20user%20code.%20Additional%20information%3A%20Access%20to%20the%20path%20%E2%80%98%5Cnetworkshare%5Cfilename.ext%E2%80%99%20is%20denied.%E2%80%9D%3C%2FEM%3E%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1598725600%22%20id%3D%22toc-hId-1598725600%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId--953431361%22%20id%3D%22toc-hId--953431361%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-789378974%22%20id%3D%22toc-hId-789378974%22%3ERoot%20Cause%3C%2FH3%3E%0A%3CP%3EIn%20the%20case%20I%20worked%20on%2C%20the%20code%20line%20that%20was%20trying%20to%20open%20the%20file%20was%20using%3CCODE%3EFile.OpenRead%3C%2FCODE%3E%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EFileStream%20fileStream%20%3D%20File.OpenRead(fileToUpload)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CCODE%3EFile.OpenRead%3C%2FCODE%3E%20tries%20to%20open%20a%20file%20with%20the%20name%20in%20the%20local%20machine%2C%20not%20in%20the%20server.%20The%20file%20is%20most%20likely%20in%20the%20server%2C%20not%20in%20the%20local%20(client)%20machines.%20Therefore%2C%20it%20throws%20an%20error%20even%20if%20you%20assigned%20perrmissions%20to%20the%20folder.%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--1762777987%22%20id%3D%22toc-hId--1762777987%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId--19967652%22%20id%3D%22toc-hId--19967652%22%3E%26nbsp%3B%3C%2FH3%3E%0A%3CH3%20id%3D%22toc-hId-1722842683%22%20id%3D%22toc-hId-1722842683%22%3ESolution%3C%2FH3%3E%0A%3CP%3EUse%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fsystem.web.httppostedfile.inputstream%3Fview%3Dnetframework-4.7.2%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EInputStream%3C%2FA%3E%26nbsp%3Binstead%20of%20File.OpenRead.%20InputStream%20will%20make%20sure%20to%20use%20the%20file%20on%20the%20server%20side.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EString%20fileToUpload%20%3D%20FileUpload2.PostedFile.FileName%3Blong%20contentLength%20%3D%20FileUpload2.PostedFile.InputStream.Length%3B%20byte%5B%5D%20buffer%20%3D%20new%20byte%5BcontentLength%5D%3B%20FileUpload2.PostedFile.InputStream.Seek(0%2C%20SeekOrigin.Begin)%3BFileUpload2.PostedFile.InputStream.Read(buffer%2C%200%2C%20Convert.ToInt32(contentLength))%3B%20Stream%20stream%20%3D%20new%20MemoryStream(buffer)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20want%20to%20save%20the%20content%20of%20stream%20to%20SharePoint%2C%20you%20can%20decode%20it%20by%20using%20the%20method%20below%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Estring%20str%20%3D%20System.Text.Encoding.ASCII.GetString(buffer)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-784516%22%20slang%3D%22en-US%22%3E%3CP%3EIf%20the%20file%20you%20are%20trying%20to%20access%20is%20not%20available%20or%20you%20don%E2%80%99t%20have%20permissions%20to%20access%20it%2C%20your%20web%20application%20(or%20SharePoint%20web%20part%20in%20my%20case)%20may%20throw%20System.UnauthorizedAccessException%20error.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

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:

 

 

FileStream fileStream = File.OpenRead(fileToUpload);

 

 

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.

 

 

String fileToUpload = FileUpload2.PostedFile.FileName;long contentLength = FileUpload2.PostedFile.InputStream.Length; byte[] buffer = new byte[contentLength]; FileUpload2.PostedFile.InputStream.Seek(0, SeekOrigin.Begin);FileUpload2.PostedFile.InputStream.Read(buffer, 0, Convert.ToInt32(contentLength)); Stream stream = new MemoryStream(buffer);

 

 

If you want to save the content of stream to SharePoint, you can decode it by using the method below:

 

 

string str = System.Text.Encoding.ASCII.GetString(buffer);