Winform 开机自动启动(C#)
来源:互联网 发布:网络批发怎么学 编辑:程序博客网 时间:2024/04/30 08:50
- private void AutoMenu_Click(object sender, EventArgs e)
- {
- string strName = Application.ExecutablePath;
- string strnewName = strName.Substring(strName.LastIndexOf("//") + 1);
- if (AutoMenu.Checked)
- {
-
- this.AutoMenu.Checked = false;
- Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
- Rkey.DeleteValue(strnewName, false);
- MessageBox.Show("程序设置完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- else
- {
- this.AutoMenu.Checked = true;
- if (!File.Exists(strName))
- return;
- Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
- if (Rkey == null)
- Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
- Rkey.SetValue(strnewName, strName);
- MessageBox.Show("程序设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }