首页 > 编程语言 >C# 实现开机自启动(原理写入注册表)

C# 实现开机自启动(原理写入注册表)

时间:2023-10-27 13:36:29浏览次数:24  
标签:MessageBox Show C# 开机 注册表 自启动 Application start

 

  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

相关文章

  • 2023-10-27 Module not found: Error: Can't resolve 'D:/xx/xx/src/pages/yyy/zzz' i
    问题描述:react+antd+umi业务中删掉了一个文件夹,重新编译报错。报错:在'D:\xx\xx\src\.umi\core'路径里面无法找到模块zzz。原因:是因为你只是把本地文件zzz给删了,但是在route.js里面还保留着它的路径,所以umi识别不到就报错了。解决方案:在route.js文件里找到zzz的路径,将之删掉再重......
  • [CF335F] Buy One,Get One Free
    气死我了,我决定水了这篇题解。反悔贪心,考虑决策及反悔,记到第三层反悔就行。然后你发现要一次只考虑一个不行,要两个两个考虑,然后就做完了,如果深入往下分析能分析出更多可以简化做法的结论。甚至可以简化到只用一层反悔,具体就是第一层可以简化到只记数量,第三层分析出可以归成第二......
  • 使用 AppDomain.CurrentDomain.GetAssemblies() 始终读取不到某一个程序集
     AppDomain.CurrentDomain.GetAssemblies() 只会获取到已加载到当前域的程序集。可以先将所有程序集加载之后再进行读取:DependencyContext.Default.RuntimeLibraries.Where(o=>o.Name.StartsWith("Yuji.")).Select(o=>Assembly.Load(newAssemblyName(o.Name))).ToArray()......
  • 'main' attribute cannot be used in a module that contains top-level code 问题解
    核心是@main注解在main.swift文件中,可以重新命名下参考资料https://stackoverflow.com/questions/73431031/swift-cli-app-main-attribute-cannot-be-used-in-a-module-that-contains-top-leve......
  • graalvm 23.1.0 独立nodejs docker 镜像&简单试用
    graaljsdocker镜像很简单就是下载官方包,集成下,然后进行一些简单的配置DockerfileFROMdebian:bullseye-backportsLABELauthor="rongfengliang"LABELemail="[email protected]"WORKDIR/opt/RUN/bin/cp/usr/share/zoneinfo/Asia/Shanghai/etc/localtime\&&am......
  • //# sourceURL=xxx
      //#sourceURL=xxx  eval(`functionadd(a,b){debugger;returna+b;};console.log(add(1,3));//#sourceURL=angdh.js`)   ......
  • 无涯教程-Clojure - cons函数
    返回一个新序列,其中"x"是第一个元素,而"seq"是其余元素。cons-语法以下是语法。(consxseq)参数   - "x"是需要添加到序列中的元素。"seq"是元素的顺序列表。返回值 - 具有附加元素的新序列。cons-示例以下是Clojure中con的示例。(nsclojure.examp......
  • `plt.subplots()`是matplotlib库中的一个函数,用于创建一个新的图形窗口和一组子图²。
    `plt.subplots()`是matplotlib库中的一个函数,用于创建一个新的图形窗口和一组子图²。以下是它的参数说明:-`nrows`:整数,默认为1。设置图表的行数¹²。-`ncols`:整数,默认为1。设置图表的列数¹²。-`sharex`,`sharey`:布尔值或{'none','all','row','col'},默认为False。控制x(s......
  • 2023CCPC女生专场 L 字符串游戏【AC自动机】
    一句话题解:AC自动机,在fail树上自顶向下预处理,以实现O(1)统计答案Description:n个模式串{Sn},1个文本串T。每次小B会选取T的一个子串(只要子串位置不相同则视作不同),对答案的贡献是该子串中含有的模式串的总数目。对于选取子串的所有方法,求总共的答案。Solution:对于文本串出现的......
  • 1.2 ICompare和IComparable
    C#排序和比较的实现1.IComparable/IComparable假设定义了一个数据,数据存放在List集合中,现在需要按自己设想的排序规则来排序如果不实现排序接口IComparablestaticvoidMain(string[]args){Personp1=newPerson("Fuor",28);Personp2=newPerson("Adain......