How to run my winform application when computer starts

user3900958 picture user3900958 · Aug 13, 2014 · Viewed 8.9k times · Source

I am just trying to run my winform application when computer starts. I have made the taskbar icon to appear on the left hand side of the system tray. My function everything is working well. But I need in such a way that if I install the winform in a computer. I need this to run after the computer restarts manually or automatically.

For now its like if I restart the application I again need to launch the application to run it. But I need something like to atuomatically launch the application on system restart. Any idea.

Codes i am trying

private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start(); 
        notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
        notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
         notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(100);
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
        this.ShowInTaskbar = true;
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Environment.Exit(0);
    }

Answer

Cyral picture Cyral · Aug 13, 2014

You can add a shortcut to your application in the startup folder, or through a registry value (Most applications use the HKLM or HKCU\Software\Microsoft\Windows\CurrentVersion\Run key in the registry or put a shortcut in the C:\Users\<user name>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder. There are other options but these are the most popular)

Sample:

Microsoft.Win32;
...

//Startup registry key and value
private static readonly string StartupKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
private static readonly string StartupValue = "MyApplicationName";

...
private static void SetStartup()
{
    //Set the application to run at startup
    RegistryKey key = Registry.CurrentUser.OpenSubKey (StartupKey, true);
    key.SetValue(StartupValue, Application.ExecutablePath.ToString());
}

You can see the results of this code in regedit: http://i.imgur.com/vYC5PPr.png

The Run key (And alternatively the RunOnce key for running your application once) will run all applications that are in it on startup/when a user logs on.

This is the code I use in my applications, and it works great. You don't need any special installer to do this, you can simply call this method every time your app starts and it will set/update the applications value in the Run key in the registry with the path to the executable.

The startup folder alternative is a little more involved to set up, check out this tutorial for help.