Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unauthorized Access Exception in Windows 7

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

like image 373
user53885 Avatar asked Jan 22 '23 19:01

user53885


1 Answers

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));
like image 107
SLaks Avatar answered Jan 29 '23 08:01

SLaks