首页 > 移动平台 > 详细

WindowsFormsApp1设置开机自启动

时间:2017-07-22 12:54:10      阅读:268      评论:0      收藏:0      [点我收藏+]
   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设置开机自启动

原文:http://www.cnblogs.com/914556495wxkj/p/7220775.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!