26

Possible Duplicate:
How to put exe file in windows Startup

Suppose I have built an application in C#, Once I install it, I want it to run in the background whenever windows starts up,or you can say a user logs in to his windows account. Is there any way I can do that? Except writing a windows service?

The application basically shows messages when a certain event is triggered Thanks

Community
  • 1
  • 1
nightWatcher
  • 1,051
  • 3
  • 15
  • 28

9 Answers9

57

Add to shortcut to Windows start-up folder:

Environment.GetFolderPath(Environment.SpecialFolder.Startup)

Or add to registry, something like this:

RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("Your App Name", "\"" + Application.ExecutablePath.ToString() + "\"");

You can change CurrentUser to LocalMachine if you want it to run with every user. Thanks to Aidiakapi.

Archie Mejia
  • 243
  • 2
  • 7
Badr Hari
  • 8,114
  • 18
  • 67
  • 100
  • 17
    Note that you could change the Registry.CurrentUser to Registry.LocalMachine to enable it for every user. +1 for example code. Another note: You could use: `@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"` instead of: `"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"` to void the escaping of the `\` character. – Aidiakapi Mar 22 '11 at 16:08
  • @Badr... Forgive me for my ignorance, do I have to put above mentioned lines of code, in my C# application code...I am lost:S. Will I be able to verify that the exe actually will start at windows startup? – nightWatcher Mar 24 '11 at 04:18
  • 1
    WPF has not ExecutablePath proprerty. – Ashish-BeJovial Dec 10 '15 at 07:48
  • How to add parameter for launch command? `myApp.exe --hidden true --fastbootmode true` – MBH Apr 08 '16 at 09:11
  • What of those is the preferred/recommended way to do this? I don' t know too much, but seems like adding it to the registry may be the proper way. – Alan Thomas Jun 02 '16 at 14:52
13

This can be done using the windows registry. I recomend you to check this registry keys.

HKLM\Software\Microsoft\Windows\CurrentVersion\Run
HKLM\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKLM\Software\Microsoft\Windows\CurrentVersion\RunServices
HKLM\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce
HKCU\Software\Microsoft\Windows\CurrentVersion\Run
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnceEx 
Jonathan
  • 11,809
  • 5
  • 57
  • 91
  • +1 For registry use, a lot more professional in my option then using the Windows startup folder. – Aidiakapi Mar 22 '11 at 16:05
  • 1
    Thank Aidiakapi :D and... Any reason for the received Downvote? – Jonathan Mar 22 '11 at 16:08
  • 1
    Probably, because he wanted votes while you were earlier. I think StackOverflow should disable the vote down on questions you've answered yourself. – Aidiakapi Mar 22 '11 at 16:09
  • I'm Agree with you. We're here for helping people, not for the points or badges. This situations should be prevented. – Jonathan Mar 22 '11 at 16:12
2

You could add your application to the registry to run on startup at

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
or
HKEY_CURREN_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

or you could add it to the startup folder for the system.

These are probably the most common/easiest options if you do not want to write a service.

Quintin Robinson
  • 81,193
  • 14
  • 123
  • 132
2

You have to set up a new key in the registry pointing to your executable.

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run

More information in this link http://msdn.microsoft.com/en-us/library/aa376977(v=vs.85).aspx

slavoo
  • 5,798
  • 64
  • 37
  • 39
Javi R
  • 2,320
  • 1
  • 17
  • 21
1

Easiest way is to put it or a shortcut to it in %userprofile%\Start Menu\Programs\Startupdirectory or %allusersprofile%\Start Menu\Programs\Startup

The registry keys HKLM\Software\Microsoft\Windows\CurrentVersion\Run (all users) and HKCU\Software\Microsoft\Windows\CurrentVersion\Run (current user only) will also serve.

Installing it as a service is often a good approach, but not if you're going to be interactive as you say.

slavoo
  • 5,798
  • 64
  • 37
  • 39
Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
0

You can write the Path to the executable in the Registry

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run

And it will get executed every time you start windows.

slavoo
  • 5,798
  • 64
  • 37
  • 39
fixagon
  • 5,506
  • 22
  • 26
0

You can put a shortcut to the application in C:\Users\@username@\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

Are you using Visual Studio Setup Project? If yes you can set the shortcut directly from there.

as-cii
  • 12,819
  • 4
  • 41
  • 43
0

Well this really sounds like you should use a "windows service".

There are other options like including a shortcut to the EXE into the "Startup" folder in the Programs Menu ("All Users" if you want it to run for all users on that system).

Another option would be to use the windows registry. You could add an entry to "Run" which points to the exe:

* HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
* HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
* HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
* HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
Patric
  • 2,789
  • 9
  • 33
  • 60
0

Since no-one else has mentioned it, I'll point out that you can also achieve this using a Scheduled Task who's trigger is 'At System Startup'. However, I haven't tried this with an app that needs UI interaction - it works for a background process, but I suspect it wouldn't work with something that needs a desktop context (since there isn't one until a user logs-on).

Eight-Bit Guru
  • 9,756
  • 2
  • 48
  • 62