问题:运行我的应用程序时使用Windows 7,未经授权的访问异常 [英] Question: Using Windows 7, Unauthorized Access Exception when running my application
问题描述
我的应用程序正在引发未经授权的访问错误。在运行我的应用程序时,我尝试访问以下位置的目录:Application.UserAppDataPath。
My application is raising an unauthorized access error. While running my application, I try to access a directory in the following location: Application.UserAppDataPath.
问题:它说我没有访问应用程序的权限。 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?
如下所示:
Application.UserAppDataPath.SetPermissions()
推荐答案
看着你的评论,你说这是你的代码:
Looking at your comment, you say this is your code:
StreamReader sr = new StreamReader(Application.UserAppDataPath);
Application.UserAppDataPath
是一个目录,而不是文件。如果您尝试直接打开它,就像尝试打开一个文件下面的文件一样,AppData文件夹,您真的没有权限。
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.
使用 Path.Combine
构建AppData文件夹内的文件的路径,即
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);
当然这只是一个例子 - 实际上你应该在AppData中使用一个子文件夹特定于您的应用程序。
Of course this is just an example - in reality you should probably be using a sub-folder inside AppData specific to your application.
这篇关于问题:运行我的应用程序时使用Windows 7,未经授权的访问异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!