UnauthorizedAccessException 与 SecurityException
FileStream 的 MSDN 构造函数表示它可能抛出 UnauthorizedAccessException 或 SecurityException。以下是 MSDN 关于这些异常的说明。
UnauthorizedAccessException: 当操作系统因 I/O 错误或特定类型的安全错误而拒绝访问时引发的异常。
安全异常: 检测到安全错误时引发的异常。
这两个相似的异常有何不同?什么情况会触发其中任何一个?
The MSDN constructor for a FileStream says that it may throw either an UnauthorizedAccessException or a SecurityException. Here's what MSDN says about these exceptions.
UnauthorizedAccessException:
The exception that is thrown when the operating system denies access because of an I/O error or a specific type of security error.
SecurityException:
The exception that is thrown when a security error is detected.
How are these two similar exceptions different? What situations will trigger either of them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当访问磁盘上的文件时出现权限错误时,会引发
UnauthorizedAccessException
。这是操作系统级别的错误,例如普通用户尝试覆盖操作系统文件(如 kernel32.dll)。如果 CLR 级别存在安全违规,则会引发
SecurityException
。例如,如果您作为低访问 ClickOnce 应用程序运行,并尝试读取/写入进程中 CLR 安全设置禁止的文件系统中的某个位置。A
UnauthorizedAccessException
is thrown when there is a permissions error accessing the file on disk. That is an error at the operating system level such as a normal user trying to overwrite an operating system file (like kernel32.dll).A
SecurityException
is thrown if there is a security violation at the CLR level. For example if you are running as a low access ClickOnce application and attempt to read / write to a place in the file system forbidden by the CLR security settings in the process.