我有一个应用程序,它会在启动时读取许可证文件。我的安装在Program Files中为应用程序创建文件夹,创建许可证文件夹,并将许可证文件放入其中。但是,当我尝试运行该应用程序时,它需要读取/更新许可证文件。当我尝试这样做的时候,我得到了一个“未经授权的访问异常”。我以管理员身份登录,并且正在手动运行该程序。
你知道为什么即使路径是正确的我也不能访问那个文件吗?但是在安装过程中,它会创建文件和文件夹吗?
我有MyApplication.exe,我的许可证阅读器在一个名为MyApplicationTools的独立DLL中。我读/写许可证文件的方式如下:
//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();谢谢
发布于 2010-01-01 05:50:08
由于UAC,您的程序没有获得管理权限。
右键单击该程序,单击“以管理员身份运行”,然后重试。
您也可以使用create a manifest that tells Windows to always run as Administrator。
但是,您应该考虑将许可证文件放在用户的AppData文件夹中,这不需要管理权限。
顺便说一句,您应该使用Path.Combine方法来创建路径。
此外,如果您只想将单个字符串写入文件,则应该调用File.WriteAllText。
例如:
File.WriteAllText(Path.Combine(path, "License.lic"), Convert.ToBase64String(sharedkey.Key));发布于 2010-01-01 06:12:34
请改用AppData。有一个用于此的环境变量。您可以通过进入资源管理器并输入%appdata%来查看。它会将您带到适当的文件夹。为了在C#中访问它,我编写了以下函数。
/// <summary>
/// Gets the path where we store Application Data.
/// </summary>
/// <returns>The Application Data path</returns>
public static string GetAppDataPath()
{
string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
dir = System.IO.Path.Combine(dir, "MyCompany\\MyApplication");
System.IO.Directory.CreateDirectory(dir);
return dir;
}发布于 2010-01-01 05:57:52
您需要将可写文件放在用户应用程序文件夹中-程序文件对于普通用户是不可写的。Iirc,在Win7上默认位置是C:\Users[username]\AppData[appname]。你不应该仅仅为了写入程序文件而以管理员身份运行。
https://stackoverflow.com/questions/1987232
复制相似问题