May 26, 2013

Open application on windows startup

At times it is required to open or start our application on windows startup. Generally on desktop if we want this feature, we will simply do the drag and drop of application to “Startup” section under “Start Menu => All Programs => Startup”. Then, in behind scenes it will automatically add a registry key under the path 

“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run”

Now assume that, we need to set to open application called “D:\TestApp.exe on startup programatically. Then we have to add the following fields in registry under the above mentioned registry path,

Name: ApplicationName
Data type: string
Value: D:\TestApp.exe (this is your application path)

Below code allows us to register our application on windows startup. For this we require namespace Microsoft.Win32.

private void AddApplicationToStartupGroup()
{
      RegistryKey registryKey = Registry.LocalMachine.
              OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            registryKey.SetValue("ApplicationName", Application.ExecutablePath);
}