用C#写的window窗体应用程序,怎样让它在开机的时候自动启动?
1个回答
展开全部
! 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; } } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询