发布网友 发布时间:2022-05-11 17:15
共1个回答
热心网友 时间:2023-10-16 01:01
! private void 启用ToolStripMenuItem_Click(object sender, EventArgs e) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.SetValue("kaixin", starupPath); MessageBox.Show("已启用开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } regset();//获取注册表状态 } private void 停用ToolStripMenuItem_Click(object sender, EventArgs e) { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); try { //SetValue:存储值的名称 run.DeleteValue("kaixin"); MessageBox.Show("已停止开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); loca.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } regset();//获取注册表状态 } private void regset() { //获取程序执行路径.. string starupPath = Application.ExecutablePath; //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装. RegistryKey loca = Registry.LocalMachine; RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); string[] subkeyNames; subkeyNames = run.GetValueNames(); foreach (string keyName in subkeyNames) { if (keyName == "kaixin") { this.开机运行ToolStripMenuItem.Text = "开机运行(启动中)"; this.启用ToolStripMenuItem.Enabled = false; this.停用ToolStripMenuItem.Enabled = true; } else { this.开机运行ToolStripMenuItem.Text = "开机运行(已停止)"; this.启用ToolStripMenuItem.Enabled = true; this.停用ToolStripMenuItem.Enabled = false; } } }