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?
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With