简体   繁体   中英

System.UnauthorizedAccess Exception C#

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

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() .

您的应用程序是否随时要求管理员特权?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM