File.WriteAllText 抛出 UnauthorizedAccessException
对远程路径执行 File.WriteAllText
会引发 UnauthorizedAccessException
。当我在记事本中打开文件时,我可以毫无问题地编辑它。尝试修改文件的进程正在以我自己的用户帐户运行,因此它应该能够访问它。
Doing File.WriteAllText
to a remote path throws UnauthorizedAccessException
. When I open the file in notepad I can edit it without a problem. The process that's trying to modify the file is running as my own user account, so it should be able to access it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果文件设置了“隐藏”属性,也会引发
UnauthorizedAccessException
。别问我为什么...The
UnauthorizedAccessException
is also thrown if the file has the "hidden" attribute set. Don't ask me why...根据MSDN,
UnauthorizedAccessException
也可以是造成的:这些情况之一是否可能是导致您出现问题的原因?
According to MSDN,
UnauthorizedAccessException
can also be caused by:Is it possible one of these conditions is the cause of your problem?
我相信如果文件被另一个进程或线程锁定,您也会遇到此异常(尽管没有记录)。
确保没有其他任何东西以禁止写入的方式打开该文件。记事本不是一个很好的测试来查看文件是否被锁定,因为它会打开锁定的文件(即:只读文件很好)。
I believe you also get this exception (although its not documented) if the file is being locked by another process or thread.
Make sure nothing else has opened the file in a manner that prohibits writing. Notepad is not a good test for seeing if a file is locked, since it will open a locked file (ie: read-only files are fine).