how can I run an app automatic after restart? (by c# code) I create A new string in 'runOnce' key in registry with the path of the App. the OS run this APP before it load the OS my problem is: My APP loads but explorer doesn't load, after I close my APP, explorer loads I restart the computer in APP, and after restart I want that my APP reopen
Asked
Active
Viewed 1.3k times
8
-
is putting a link to your app in the startmenu->autostart folder not working? – juergen d Sep 20 '11 at 09:54
-
@juergend I believe he means restart of the application, not OS. – Archie Sep 20 '11 at 09:58
-
1What is your problem? RunOnce is the correct solution and according to your question you already use it... – Daniel Hilgarth Sep 20 '11 at 09:58
-
Taking everything into account... Doesn't the term "restart" mean that the app is already running? – spender Sep 20 '11 at 10:04
-
He means the restart of the OS. – Maxim V. Pavlov Sep 20 '11 at 10:07
-
@Daniel Hilgarth - my problem is: My APP loads but explorer doesn't load, after I close my APP, explorer loads – sari k Sep 20 '11 at 10:09
-
@sari: that's a real essential piece of information. Please update your question with it. – Daniel Hilgarth Sep 20 '11 at 10:11
3 Answers
10
When you click restart from your app, make the following modifications to the registry:
Create an entry in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
registry branch.
Use
Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");
to create an entry.
And
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName", true);
myKey.SetValue("YourAppName", "AppExecutablePath", RegistryValueKind.String);
to set the run path.
After the system has restarted, your app starts and removes the restart entry by calling this:
Registry.LocalMachine.DeleteSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\YourAppName");

Maxim V. Pavlov
- 10,303
- 17
- 74
- 174
-
I mean to start an app after the OS restart, but I want that it do it only if I do restart by an APP ( not start an APP always) – sari k Sep 20 '11 at 10:15
-
@sarik just use the technique descriped by , plus add the entry when you're shutting down from withhin your application and during application startup delete that RegistryKey – yas4891 Sep 20 '11 at 10:17
-
I have updated an answer to reflect your need to cancel consequent app restarts after the first restart. – Maxim V. Pavlov Sep 20 '11 at 10:19
-
wouldnt the runonce key be better? is there some reason you did not choose that? – Simon Apr 14 '14 at 01:56
-
1You should not create a SubKey, only need to set the value inside of Run. – Vippy Sep 10 '14 at 00:26
-
-
The above example lines are c# code. You write them in your .net app to enable auto restart via registry manipulations. – Maxim V. Pavlov Jul 01 '15 at 10:32
-
4
It seems like your best bet would be to add your program to RunOnce, instead of Run. That way it will be started after the next reboot, but you won't have to worry about erasing the key afterwards.
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce

Christopher B. Adkins
- 3,499
- 2
- 26
- 29
-
"RunOnce" is usually a bad idea because if some unforeseen issue happens and the key is automatically removed, you've lost the ability to run whatever you added in the first place. Better to use "Run" and delete it later after some validation. – Vippy Sep 10 '14 at 00:25
-
1Using that logic, Run would also be a bad idea since if some unforeseen issue happens the key is not removed then your program would run every time the computer restarts. "RunOnce" has less maintenance since you just write it and forget it, "Run" allows for more control since you can remove it once it is no longer needed instead of re-writing it if the problem still exists after restart. I would call this a personal taste issue. – Christopher B. Adkins Sep 15 '14 at 08:25
-
I personally use Run with flag files or a DB to track it's progress. I can see your point, it's just difficult if the app is no longer being executed. Also, depends on how critical the app is on being executed (like mine). – Vippy Sep 15 '14 at 19:20
2
This is a better answer as you should not create a SubKey. Also this will automatically dispose.
string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(runKey, true))
{
key.SetValue("MyProgram", @"C:\MyProgram.exe");
}

Vippy
- 1,356
- 3
- 20
- 30