Winform 开机自动启动(C#)

来源:互联网 发布:网络批发怎么学 编辑:程序博客网 时间:2024/04/30 08:50
  1. private void AutoMenu_Click(object sender, EventArgs e)
  2.         {
  3.             string strName = Application.ExecutablePath;
  4.             string strnewName = strName.Substring(strName.LastIndexOf("//") + 1);
  5.             if (AutoMenu.Checked)
  6.             {
  7.                 //修改注册表,使程序开机时不自动执行。 
  8.                 this.AutoMenu.Checked = false;
  9.                 Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
  10.                 Rkey.DeleteValue(strnewName, false);
  11.                 MessageBox.Show("程序设置完成!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  12.             }
  13.             else
  14.             {
  15.                 this.AutoMenu.Checked = true;
  16.                 if (!File.Exists(strName))//指定文件是否存在 
  17.                     return;
  18.                 Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run"true);
  19.                 if (Rkey == null)
  20.                     Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
  21.                 Rkey.SetValue(strnewName, strName);//修改注册表,使程序开机时自动执行。 
  22.                 MessageBox.Show("程序设置完成,重新启动计算机后即可生效!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  23.             }
  24.         }