25 May 2008
Autostarting your WinForms application
Posted by Mikhail Esteves under: C#; Tips .
Here’s a helper function to add your Winforms application to the Windows startup list:
public static void AddAutoStartProgram(string name, string appPath) { string runKeyBase = @"Software\Microsoft\Windows\CurrentVersion\Run\"; RegistryKey rk = Registry.CurrentUser .OpenSubKey(runKeyBase + name);if (rk == null) { RegistryKey runKey = Registry.CurrentUser .CreateSubKey(runKeyBase); runKey.SetValue(name, appPath); runKey.Close(); } else { rk.SetValue(name, appPath); rk.Close(); } }
Usage is simple: AddAutoStartProgram("myapp", Application.ExecutablePath);
To remove a program from automatically starting up, just delete the key:
public static void RemoveAutoStartProgram(string name) { string runKeyBase = @"Software\Microsoft\Windows\CurrentVersion\Run";RegistryKey rk = Registry.CurrentUser.OpenSubKey(runKeyBase, true); rk.DeleteValue(name, false); rk.Close(); }
Remove your app with a call like RemoveAutoStartProgram("myapp");