Run your application at start up

introduction


this code will help you to add your application to the windows startup.

namespace


namespace used for this is Microsoft.Win32 which will be imported to your application using:

using Microsoft.Win32;

at the top of the file.

code and its description



The registry key is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to all users and HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to the current user, which contains entries for the softwares to be loaded at startup

in this example i have added a checkbox to be checked or unchecked if the user wants to add or remove application from startup, respectively. and a button to process the selection.


private void BtnAddRemove_Click(object sender, EventArgs e)
{
//to have access to HKEY_LOCAL_MACHINE and if u need to add only for HKEY_CURRENT_USER,
//use CurrentUser instead of LocalMachine
RegistryKey rk = Registry.LocalMachine;
RegistryKey mykey;

//access the regitry key where you want to add your registry key
mykey= rk.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
if (chkAddRemove.Checked == true )
{
//check if same registry value exists or not ......and create only if it is not there
if (mykey.GetValue("myApp") == null)
{
mykey.SetValue("myApp", Application.ExecutablePath, RegistryValueKind.ExpandString);
}

//dislpay message box if registry is already there
else
{
MessageBox.Show("The entry already exists");
}
}

//delete if the user has uncked the checkbox....and the value exists in the registry
else
{
if (mykey.GetValue("myApp") != null)
mykey.DeleteValue("myApp");
}
}


Comments

Author: Hefin Dsouza31 Jul 2008 Member Level: Gold   Points : 0

Good Coding
Thanks

Regards Initiotech



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: