Question: Using Windows 7, Unauthorized Access Exception when running my application

只谈情不闲聊 提交于 2019-12-10 14:58:48

问题


My application is raising an unauthorized access error. While running my application, I try to access a directory in the following location: Application.UserAppDataPath.

The Problem: It says I do not have permission to access the Application.UserAppDataPath directory

Is there a way to set permissions within my application source code?

Something like:

Application.UserAppDataPath.SetPermissions()

回答1:


Looking at your comment, you say this is your code:

StreamReader sr = new StreamReader(Application.UserAppDataPath);

Application.UserAppDataPath is a directory, not a file. If you try to open that directly, it's the same as trying to open a file one level below the AppData folder, which you really don't have permission to do.

Use Path.Combine to construct a path to a file inside the AppData folder, i.e.

string fileName = Path.Combine(Application.UserAppDataPath, "settings.xml");
StreamReader sr = new StreamReader(fileName);

Of course this is just an example - in reality you should probably be using a sub-folder inside AppData specific to your application.




回答2:


Its probably a UAC issue, Try running your application as an elevated process, and see if the error persists



来源:https://stackoverflow.com/questions/2376430/question-using-windows-7-unauthorized-access-exception-when-running-my-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!