tags:

views:

256

answers:

8

How do I configure a C# program to run when the operating system is first started?

+8  A: 

Just add the program to the Startup folder in the Start Menu.

Timwi
+4  A: 

add to registry

private void AddToRegistry()
        {
            RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            regKey.SetValue(Application.ProductName, Application.ExecutablePath);
        }
anishmarokey
Wouldn't that add it every time this method is executed? Shouldn't you only want to do it once?
George Stocker
"Wouldn't that add it every time this method is executed" - no, regKey.SetValue will overwrite the value each time it is executed.
Joe
@George , yes, it add the key to the registry every time the method is executed, but it "could" be "bydesign" if you want to be sure that users dont delete the key. It does not create a new key, just update the key if it exists and create the key if it does not exist. And If you run AddToRegistry only at the program startup then its no problem. (Some users may be angry that the program recreate the key if the have manually deleted it, though)
Stefan
-1 because you don't dispose the registry key.
erikkallen
thank's for the help, but how i delete this from the registry ?
Gold
A: 

The easiest answer would be to add it to the Startup folder in your Start Menu. As far as I know simply dropping it in there should be enough (since it is just a little exe).

Adkins
+13  A: 

If you want to run the program when the user logs on, then the "Startup" folder or Run registry key methods both work.

If you want the program to run when the computer is turned on (ie Windows starts), without waiting for the user to log on, you will need to install it as a service, and configure it to start automatically.

Nader Shirazie
+1  A: 

I believe you are going to have to convert this C# application into a Windows service and set its startup type to Automatic. That seems to be the approach most people use.

Brian Gideon
A: 

For a simple C# app, putting the app's .exe or a shortcut to it, in the start folder is the easiest approach.

To build it in a little bit more, you could add it to the registry, under "Software\Microsoft\Windows\CurrentVersion\" then the subkey that you require. For more info on the registry approach read this - http://support.microsoft.com/kb/179365

For a more complex approach which may/maynot be needed depending on your application you can create it as a service, and have it set to run automatically. For a simple app this isn't needed so I won't expand further on this point.

JonWillis
A: 

You can develop this program as a Windows service. Then you can configure it to re-start after a failure or subsequent failures which increases robustness. Just an idea...

Zafer
A: 

I'm using Inno Setup for my installer and adding the following line will accomplish this:

Name: "{commonstartup}\YourFolder"; Filename: "{app}\YourApp.exe"; IconFilename: "{app}\YourApp.ico"

Also add this to your [setup] section

PrivilegesRequired=admin

Documentation http://www.jrsoftware.org/iskb.php?startup

Chuck