简体   繁体   中英

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

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()

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, ie

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.

它可能是一个UAC问题,尝试将您的应用程序作为升级过程运行,并查看错误是否仍然存在

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM