Running Programs on Startup in C#
It must have occurred to you that it'd be super cool to run your program on startup. Not by moving the program to the startup folder, but by using a friendly checkbox that would set this up.
In the registry (regedit.exe
), you can find
under
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
individual keys that specify the path to an executable file to run automatically
on startup. So it'll be nothing more than working with the registry
Let's create a new project. We'll insert a
checkbox with a reasonable label into the form and add the
CheckedChanged
event to it.
Now let's create a RegistryManager
class.
We'll add using Microsoft.Win32;
and we'll
declare a class variable for access to programs that run on startup.
static RegistryKey onStartup = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
Now we'll create a method for setting/removing the program to run on startup.
using System.Windows.Forms;
public static void SetOnStartup(bool run) { if (run == true) rkApp.SetValue("MyApplication", Application.ExecutablePath.ToString()); else rkApp.DeleteValue("MyApplication", false); }
If the run
variable is true, a key named
"MyApplications" is set in the registry. You can also use
Application.ProductName
, which returns the application name setting
in Properties\AssemblyInfo.cs
. If not, the value will be deleted.
The second parameter (false in our case) determines whether an exception should
be thrown in case of an error while deleting (the key doesn't exist), which we
don't need.
Now we'll only add this line to the
CheckedChanged
event:
RegistryManager.SetOnStartup(runOnStartup.Checked);
Now the program seems to be done. But you'll surely think of how to check the checkbox automatically. Saving to a file would be possible, but what if the user deleted the key from the registry manually? Therefore, it's better to create another method for this, which will see if the key exists in the registry.
So we'll add another method in RegistryManager
named something like IsOnStartup. And simply:
return (rkApp.GetValue(Application.ProductName) != null);
You probably won't think of that, but what happens when the user moves that file to another folder? Well, nothing at all. And literally. The program simply doesn't start because the system won't find it at the given path. This can be easily fixed. We'll ge t the path from the registry and see if the file exists.
The whole method will then look something like this:
public static bool IsOnStartup() { if (rkApp.GetValue(Application.ProductName) == null) return false; if (!System.IO.File.Exists(rkApp.GetValue(Application.ProductName).ToString())) rkApp.SetValue(Application.ProductName, Application.ExecutablePath.ToString()); return true; }
And then we'll simply call it in the Form constructor.
runOnStartup.Checked = RegistryManager.IsOnStartup();
And we're done. Now let's try it. I wonder how many of you will restart your computer because of this I'm already running Virtual PC to try the example (now I gave you an idea)
I'll add a small note, and that is using parameters. If you want to run the program minimized or in the tray, simply store the path with, for example, the "-h" parameter (stands for hidden) in the registry. "C:\.....\MyProgram.exe -h"
Have you noticed that in the program constructor, i.e. in
Program.cs there is String[] args
by default? So
now you'll see what it's good for
Form1 form = new Form1(); if ((args.Length > 0) && (args[0] == "-h")) { form.WindowState = FormWindowState.Minimized; } Application.Run(form);
Alternatively, if you're using a tray icon, you can also write
form.ShowInTaskbar = false;
. If you're displaying it manually, then
check the FormWindowState form in the constructor and display it accordingly.
Simple, isn't it?
Download
By downloading the following file, you agree to the license terms
Downloaded 62x (41.02 kB)
Application includes source codes in language C#