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