Start application at Windows startup with C#
The following snippet will allow you to add your application in the registry so it will launch when Windows start. Alternative you can use Environment.SpecialFolder.Startup
to place a shortcut of your application in the startup folder which will have the same effect.
Note that this snippet will add an entry in HKEY_CURRENT_USER
which means the program will only launch at startup for the user that is currently logged in when you run the code. If you want your program to run at startup for all users you will need to use HKEY_LOCAL_MACHINE
instead but keep in mind that you will require administration rights in order to do that.
Register program to start with Windows:
1 2 3 4 5 6 7 | public static void AddApplicationToStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.SetValue("My Program", "\"" + Application.ExecutablePath + "\""); } } |
Stop program from starting with Windows:
1 2 3 4 5 6 7 | public static void RemoveApplicationFromStartup() { using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) { key.DeleteValue("My Program", false); } } |
here it is as a C# property.
How can I make it run with adminitrative privileges?
How to Identify this autorun or manually triggered?
the register will be one time or every time on load , suppose i call add function 1 time on load then app start then i remove the call to fucntion from loadform
There is no need to remove it, except if you want to.
Usually applications allow the users to decide if they want the program to start on Windows startup or not.
This means you will need to read from the registry if your program is already set to start or not, then simply allow the user toggle on/off while you execute the appropriate method in the background.
Nice job, it’s a great post. The info is good to know!