曹永思

导航

WinForm开机启动 判断 设置



        /// <summary>
        /// 判断是否是开机启动
        /// </summary>
        private void CheckIsKJJD()
        {
            //声明注册表LocalMachine对象

            RegistryKey R_local = Registry.LocalMachine;
            //创建注册表项
            RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
            //判断获取的值是否为空
            if (R_run.GetValue("AqiooWebQQ") == null)
            {
                //为空,不勾选控件
                checkBox1.Checked = false;
            }
            else
            {
                //不为空,勾选
                checkBox1.Checked = true;
            }
            //关闭注册表对象
            R_run.Close();
            //关闭注册表对象

            R_local.Close();
        }

        //CheckBox控件选择事件
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //获得应用程序启动路径
            string R_startPath = Application.ExecutablePath;
            //判断checkBox1控件是否选中
            if (checkBox1.Checked == true)
            {
                //声明注册表LocalMachine对象

                RegistryKey R_local = Registry.LocalMachine;
                //创建注册表项

                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                //设置键AqiooWebQQ的值为变量R_startPath
                R_run.SetValue("AqiooWebQQ", R_startPath);
                //关闭注册表对象
                R_run.Close();
                //关闭注册表对象

                R_local.Close();
            }
            else
            {
                try
                {
                    //声明注册表LocalMachine对象

                    RegistryKey R_local = Registry.LocalMachine;
                    //创建注册表项

                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    //删除键BirthdayyTipF
                    R_run.DeleteValue("AqiooWebQQ", false);
                    //关闭注册表对象

                    R_run.Close();
                    //关闭注册表对象

                    R_local.Close();
                }
                catch (Exception ex)//捕捉错误
                {
                    //弹出提示框
                    MessageBox.Show("您需要管理员权限修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }

            }
        }

posted on 2012-11-08 14:52  曹永思  阅读(454)  评论(0编辑  收藏  举报