.NET下载文件报错System.UnauthorizedAccessException的解决方法 |
您所在的位置:网站首页 › workpass下载 › .NET下载文件报错System.UnauthorizedAccessException的解决方法 |
转自原文.NET下载文件报错System.UnauthorizedAccessException的解决方法
假设VS代码对应路径为E:\Projects\Web1,在VS用“发布Web”的方式发布后的路径为E:\Site\Web1。 在IIS新建2个站点,站点A指向E:\Projects\Web1,站点B指向E:\Site\Web1。 现在出现一个异常情况,站点B能正常下载123.xls,站点A下载时却提示错误: System.UnauthorizedAccessException: 对路径“E:\Projects\Web1\Download\123.xls”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream..ctor(String path, FileMode mode) 站点A和站点B的目录和文件权限一模一样,特意为站点A增加了权限等还是无效,搜索了一下,最后找到了解决方法,把下载代码里面的 FileStream fs = File.Open(filePath, FileMode.Open) 改为就可以下载了。 FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read); 官方MSDN文档如下: File.Open 方法 (String, FileMode) 打开指定路径上的 FileStream,具有读/写访问权限。 异常 UnauthorizedAccessException path 指定了一个只读文件。 - 或 - 在当前平台上不支持此操作。 - 或 - path 指定了一个目录。 - 或 - 调用方没有所要求的权限。 - 或 - mode 为 Create,指定文件为隐藏文件。 这时候回过头再去看E:\Projects\Web1\Download\123.xls,果然是只读属性, 而E:\Site\Web1\Download\123.xls没有只读属性; 把E:\Projects\Web1\Download\123.xls的只读属性去掉,重新恢复刚开始的代码 FileStream fs = File.Open(filePath, FileMode.Open); 这时下载就正常。 总结,解决方法有2种: 1、把文件的只读属性去掉; 2、FileStream fs = File.Open(filePath, FileMode.Open)改用下面这行代码 FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read);
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |