. NET download file error System.UnauthorizedAccessException solution

Source: Internet
Author: User
Tags file permissions

Transferred from the original text. NET download file error System.UnauthorizedAccessException solution

Assume that the VS code corresponds to a path of E:\Projects\Web1, and that the path after vs is published in the "Publish Web" mode is E:\Site\Web1.
In IIS new 2 sites, site A points to E:\Projects\Web1, and site B points to E:\Site\Web1.

Now there is an abnormal situation, site B can download 123.xls, site a download when the error is prompted:

System.UnauthorizedAccessException: Access to Path ' E:\Projects\Web1\Download\123.xls ' is denied.
In System.io.__error.winioerror (Int32 errorCode, String Maybefullpath)
In System.IO.FileStream.Init (String path, FileMode mode, FileAccess access, Int32 rights, Boolean Userights, FileShare Shar E, Int32 buffersize, fileoptions options, Security_attributes secattrs, String Msgpath, Boolean bfromproxy, Boolean Uselon Gpath, Boolean Checkhost)
In System.IO.FileStream. ctor (string path, FileMode mode, FileAccess access, FileShare share, Int32 buffersize, fileoptions options, String Msgpath , Boolean Bfromproxy)
In System.IO.FileStream. ctor (String path, FileMode mode)

Site A and Site B directory and file permissions exactly the same, specifically for the site a added permissions, such as or invalid, search, and finally found a solution, the download code inside the
FileStream fs = File.Open (FilePath, FileMode.Open)
Instead, you can download it.
FileStream fs = File.Open (FilePath, FileMode.Open, FileAccess.Read);


The official MSDN documentation is as follows:

File.Open method (String, FileMode)
Opens the FileStream on the specified path with read/write access.

Exception UnauthorizedAccessException

PATH Specifies a read-only file.
Or
This operation is not supported on the current platform.
Or
path Specifies a directory.
Or
The caller does not have the required permission.
Or
Mode is Create and specifies that the file is a hidden file.


At this time go back to see E:\Projects\Web1\Download\123.xls, sure enough is a read-only attribute,
And E:\Site\Web1\Download\123.xls has no read-only attribute;

Remove the read-only attribute from the E:\Projects\Web1\Download\123.xls and restore the code that just started
FileStream fs = File.Open (FilePath, FileMode.Open); The download is normal at this time.

Summary, there are 2 ways to solve the problem:
1. Remove the read-only attribute of the file;
2, FileStream fs = File.Open (FilePath, FileMode.Open) use the following line of code
FileStream fs = File.Open (FilePath, FileMode.Open, FileAccess.Read);

. NET download file error System.UnauthorizedAccessException solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.