码迷,mamicode.com
首页 > Windows程序 > 详细

C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)

时间:2016-08-31 13:46:58      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

#region//开机自动运行
        private void CB_Auto_CheckedChanged(object sender, EventArgs e)
        {//CB_Auto是一个Checkbox,IsAutoRun 是个布尔变量,用于控制是否开机运行
            if (CB_Auto.Checked == true) IsAutoRun = true;
            else IsAutoRun = false;
            try
            {
                AutoRun();
            }
            catch
            { }            
        }
        private void AutoRun()
        { 
            //获取程序执行路径..
            string starupPath = Application.ExecutablePath;
            //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.
            RegistryKey loca = Registry.LocalMachine;
            RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
           
            try
            {
                 //SetValue:存储值的名称
                if (IsAutoRun == false) run.SetValue("WinForm", false);//取消开机运行
                else run.SetValue("WinForm", starupPath);//设置开机运行
                loca.Close();
            }
            catch
            {}

        }
        #endregion

//判断程序是否已经设置成开机自动启动,在form_load中写入

RegistryKey loca_chek = Registry.LocalMachine;
                RegistryKey run_Check = loca_chek.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (run_Check.GetValue("WinForm").ToString().ToLower() != "false")
                {/分别/对应上面的WinForm和false
                    CB_Auto.Checked = true;
                }
                else
                {
                    CB_Auto.Checked = false;
                }

C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)

标签:

原文地址:http://www.cnblogs.com/candyzhmm/p/5825309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!