简体   繁体   English

问题:在运行我的应用程序时使用Windows 7,未经授权的访问异常

[英]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. 在运行我的应用程序时,我尝试访问以下位置中的目录:Application.UserAppDataPath。

The Problem: It says I do not have permission to access the Application.UserAppDataPath directory 问题:它表示我没有访问Application.UserAppDataPath目录的权限

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 . Application.UserAppDataPath是一个目录 ,而不是一个文件 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. 如果试图直接打开,这是一样试图打开下面 AppData文件夹一个文件一个级别,你真的没有权限做。

Use Path.Combine to construct a path to a file inside the AppData folder, ie 使用Path.Combine构建AppData文件夹内文件的路径,即

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. 当然这只是一个例子 - 实际上你可能应该在AppData中使用特定于你的应用程序的子文件夹。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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