[Issue]windows 7 下创建FileStream 时出现 UnauthorizedAccessException

 

[Resolution]

之前曾使用在其他盘创建一个文件夹,文件夹的security允许everyone来进行Full control,然后把文件创建在这个文件夹下面,即使是这样也仍然会出现UnauthorizedAccessException。

最后的解决方案是把文件创建在ApplicationData下面就不会出现UnauthorizedAccessException。

 StringBuilder folder = new StringBuilder(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

 

stackoverflow网站上查了下UnauthorizedAccessException应该改是操作系统抛出来的异常,这样说来,win7 或者vista只能在ApplicationData下面创建用户自己的文件。

2 vote down check

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.

 

 

 

 

posted on 2010-05-09 12:50  gracestoney  阅读(1137)  评论(2编辑  收藏  举报