0
votes

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);
    registryKey.SetValue("Quick", Process.GetCurrentProcess().MainModule.FileName.ToString());
}
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) (....)

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.

1
Do you run your application as administrator? - user3026017
Hello, yes I run it as administrator - harili
@harili, based on my test, I can set the value successfully. Then, where do you see the error, do you throw a exception or other else? I suggest that you can put your app in D disk to have a try. - Jack J Jun - MSFT
@JackJJun-MSFT Hello, I can also set the key successfully. When I turn on the computer (to test if the app is going to launch itself alone), the exception abore appears. Yes I throw an exception and I've try to ad a pop-up which suggests you to run the app with administrator access, but no success. Then I am going to try to put it into my D:. - harili

1 Answers

0
votes

Finnaly I got a solution but it's a little different. I didn't create a key in the registry due to the lack of autorization. To overcome this problem, I choose to create a shortcut directly in the startup folder (CTRL+R and shell:startup if you want to see it).