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

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

#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设置/取消开机自动运行,判断程序是否已经设置成开机自动启动(转载)