Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I am trying to launch automatically my application when I turn on the computer. I've wrote this program into my code :

if (Convert.ToInt32(_param.AUTOMATIC_LAUNCH) == 1)
    registryKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Convert.ToInt32(_param.AUTOMATIC_LAUNCH) == 0 && registryKey != null)
    registryKey.DeleteValue("Quick", true);

The key is indeed in the folder : "Software\Microsoft\Windows\CurrentVersion\Run". But I have a this message which appears in my log :

System.UnauthorizedAccessException: access to the path 'C:\WINDOWS\System32\config_borne.ini' denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) (....)

What I have tried:

I've tried to grant autorization to the user in the folder of my application but it doesn't work. In addition, the "config-borne.ini" is a file which doesn't exist.
Updated 28-Oct-20 17:12pm
BillWoodruff 28-Oct-20 19:02pm
where exactly is the error occurring ?

1 solution

Everything under Windows is ReadOnly to everyone except admins.

On top of that, assuming this .INI file is indeed part of your app, your app would also need to use the users admin permissions, so you're going to have to add an app.manifest to your project to enable that.

But, an even better solution would be to put your .INI file somewhere more appropriate than under the Windows folder, like under C:\ProgramData.

And even better than that, why are you even using an .INI file when there are better alternatives, like XML or JSON?

And thinking about it a little more, it may even be that you're not using fully qualified paths when specifying your file to read/write. That's going to be a problem because your code makes the assumption of what the "Current Directory" is, and that can change when you're not expecting it. ALWAYS build fully qualified paths to files built from well-known locations. Google for "C# Environment.GetFolderPath" for more information. Be sure to look at the link to Environment.SpecialFolder documentation for what to pass into that method call.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900