private void chk开机自启动_CheckedChanged(object sender, EventArgs e) { if(chk开机自启动.Checked==true) { if(MessageBox.Show("确定【进行】开机自启动?")==DialogResult.OK) { AutoPC(true); } } else if (chk开机自启动.Checked == false) { if (MessageBox.Show("确定【取消】开机自启动?") == DialogResult.OK) { AutoPC(false); } } } /// <summary> /// 开机自启动 /// </summary> /// <param name="isOK">true为开机自启动,false为取消开机自启动</param> private void AutoPC(bool isOK) { if (isOK && !GetRegistData(Path.GetFileNameWithoutExtension(Application.ExecutablePath))) { MessageBox.Show("设置开机自启动,需要修改注册表", "提示"); string path = Application.ExecutablePath; RegistryKey rk = Registry.CurrentUser; // // 添加到 当前登陆用户的 注册表启动项 try { //系统统启动等待时间 int NumIn = 5000; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("start_processMgr", Application.StartupPath + "\\ini\\start_processMgr.bat"); MessageBox.Show("添加成功"); rk2.Close(); rk.Close(); File.WriteAllText(Application.StartupPath + "\\ini\\start_processMgr.bat", @"C:\Windows\System32\cmd.exe /c timeout " + NumIn + "&start " + '"' + '"' + " start " + '"' + "启动" + '"' + ' ' + '"' + Application.StartupPath + "\\ProcessMgr.exe" + '"' + "\r\n" + "exit", Encoding.Default); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { //取消开机自启动 MessageBox.Show("取消开机自启动,需要修改注册表", "提示"); string path = Application.ExecutablePath; RegistryKey rk = Registry.CurrentUser; try { RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("start_processMgr", false); MessageBox.Show("取消成功"); rk2.Close(); rk.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private bool GetRegistData(string name) { string registData; RegistryKey hkml = Registry.CurrentUser; RegistryKey software = hkml.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true); if (software.GetValue(name) == null) { return false; } registData = software.GetValue(name).ToString(); string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\ini\\start_processMgr.bat"; if (registData.Contains(path)) { return true; } else { return false; } }
标签:MessageBox,Show,C#,开机,注册表,自启动,Application,start From: https://www.cnblogs.com/yuanshuo/p/17792139.html