views:

541

answers:

2

Hi,

I currently have a compiled C# program but whenever I run it I get the Windows encountered a problem error.

This is from a System.UnauthorizedAccess error, how can I give access and remove this error without any need from the user side, since this program is being deployed to a lot of people and I don't want them having to make this fix manually.

Thanks

A: 

Does your application ask for Administrator's privileges at any time?

bobber205
Yes it does ask for admin privileges, a lot of my users found that it happened when opening the application. And if the application doesn't find the config file on start then it creates it therefore it may be having problems creating this in the program files environment.
Sandeep Bansal
Well there's your problem. Under UAC you cannot create files in Program Files without elevating. Nor should you be doing it there anyway, there are better places to keep global settings.
blowdart
O ok then, How would I put the config file somewhere decent like in appdata?
Sandeep Bansal
+1  A: 

You can get the current user's application data folder using the environment variable APPDATA. Therefore, you can do something like:

string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
string configFile = Path.Combine(appdata, configFile);
StreamWriter writer = new StreamWriter(configFile);
writer.WriteLine("my config data");
writer.Close();

You can also use this approach to get the temporary folder as well. You can even generate a random file name using the BCL functions. I think it's Path.GetTempFilename().

atanamir