WindowsFormsApp1设置开机自启动

Posted 无懈可击

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WindowsFormsApp1设置开机自启动相关的知识,希望对你有一定的参考价值。

   private void Form1_Load(object sender, EventArgs e)
        {
            SetAutoBootStatu(true);
        }

  

 

 

/// <summary>  
        /// 在注册表中添加、删除开机自启动键值  
        /// </summary>  
        public static int SetAutoBootStatu(bool isAutoBoot)
        {
            try
            {
                //RegistryKey rk = Registry.LocalMachine;
                //RegistryKey rk2 = rk.CreateSubKey("Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run");
                //rk2.SetValue("MyExec", execPath);
                string execPath = Application.ExecutablePath;
                RegistryKey rk = Registry.LocalMachine;
                RegistryKey rk2 = rk.CreateSubKey("Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Run");
                if (isAutoBoot)
                {
                    rk2.SetValue("MyExec", execPath);
                    Console.WriteLine(string.Format("[注册表操作]添加注册表键值:path = {0}, key = {1}, value = {2} 成功", rk2.Name, "TuniuAutoboot", execPath));
                }
                else
                {
                    rk2.DeleteValue("MyExec", false);
                    Console.WriteLine(string.Format("[注册表操作]删除注册表键值:path = {0}, key = {1} 成功", rk2.Name, "TuniuAutoboot"));
                }
                rk2.Close();
                rk.Close();
                return 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("[注册表操作]向注册表写开机启动信息失败, Exception: {0}", ex.Message));
                return -1;
            }
        }

  

以上是关于WindowsFormsApp1设置开机自启动的主要内容,如果未能解决你的问题,请参考以下文章

win10怎么设置开机自启动软件(win10怎么设置开机自启动软件)

设置linux设置开机自启动应用程序

开机自启脚本

设置windows服务开机自启动

电脑上开机自启动在哪里设置添加

mac设置shell脚本开机自启动