Windows 7中的未授权访问异常 [英] Unauthorized Access Exception in Windows 7

查看:342
本文介绍了Windows 7中的未授权访问异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序在启动时它读取许可文件。我安装在创建Program Files文件夹应用程序,创建文件夹的许可,并把许可证文件在那里。然而,当我尝试和运行应用程序,它需要读取/更新许可文件。当我尝试这样做,我得到一个未经授权的访问异常。我登录作为管理员,我手动运行该程序。

I have an application which reads a license file when it starts up. My install creates the folder in Program Files for the application, creates the license folder and puts the license file in there. However when I try and run the application, it needs to read/update the license file. When I try and do that I get an "Unauthorized Access Exception". I am logged on as an administrator and I'm running the program manually.

任何想法,为什么我不能访问该文件,即使路径是正确的?但在安装它创建的文件和文件夹就好了?

Any idea why I cannot access that file even though the path is correct? But in the install it creates the file and folder just fine?

我有MyApplication.exe,和我的执照读者在叫MyApplicationTools一个单独的DLL。我读/写这样的许可文件,以便:

I have MyApplication.exe, and my license reader is in a seperate DLL called MyApplicationTools. I am reading/write the license file like so:

       //Read
       StreamReader reader = new StreamReader(path + "license.lic");

       //Write
       StreamWriter writer2 = new StreamWriter(path + "License.lic");
       string str = Convert.ToBase64String(sharedkey.Key);
       writer2.WriteLine(str);
       writer2.Close();



感谢

Thanks

推荐答案

,你的程序是没有得到管理员权限。

Because of UAC, your program isn't getting administrative privileges.

右键单击该程序,然后单击以管理员身份运行,然后再次尝试。

你也可以创建一个清单,告诉Windows来始终以管理员身份运行。结果
然而,你应该考虑把在用户的AppData文件夹,不需要管理权限许可文件。

Right-click the program, click Run as Administrator, and try again.
You can also create a manifest that tells Windows to always run as Administrator.
However, you should consider putting the license file in the user's AppData folder, which does not require administrative privileges.

顺便说一句,你也应该使用 Path.Combine 方法来创建路径。结果
,如果你只想写一个字符串到一个文件,你应该叫 File.WriteAllText 结果
,例如:

By the way, you should use the Path.Combine method to create paths.
Also, if you just want to write a single string to a file, you should call File.WriteAllText.
For example:

File.WriteAllText(Path.Combine(path, "License.lic"), Convert.ToBase64String(sharedkey.Key));

这篇关于Windows 7中的未授权访问异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆