闫振

博客园 首页 联系 订阅 管理
#region     设置开机自动启动程序
        //读注册表数据信息
        public string GetRegistData(string name)   //键名称
        {
            string registData;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
            RegistryKey aimdir = software.OpenSubKey(ReigstidFileName, true);
            registData = aimdir.GetValue(name).ToString();
            return registData;
        }
         //写注册表数据信息,分别是子键名称和值存放LocalMachine目录下的XXX文件中,其文件名为name的值,值为tovalue的值
        public bool WTRegedit(string name, string tovalue)
        {
            bool WriteOk = false;
            try
            {
                RegistryKey hklm = Registry.LocalMachine;
                RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
                RegistryKey aimdir = software.CreateSubKey(ReigstidFileName);
                aimdir.SetValue(name, tovalue);
                WriteOk = true;
            }
            catch
            {
                WriteOk = false;
            }
            return WriteOk;
        }
        //修改注册表数据信息
        public bool EditRegedit(string name, string tovalue)
        {
            bool EditOk = false;
            try
            {
                if (IsRegeditExist(name))
                {
                    RegistryKey hklm = Registry.LocalMachine;
                    RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
                    RegistryKey aimdir = software.CreateSubKey(ReigstidFileName);
                    aimdir.SetValue(name, tovalue);
                    EditOk = true;
                }
            }
            catch
            {
                EditOk = false;
            }
            return EditOk;
        }
        //删除注册表数据信息
        public bool DeleteRegist(string name)
        {
            bool DeleteOk = false;
            string[] aimnames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(RegeditPath, true);
            RegistryKey aimdir = software.OpenSubKey("Run", true);
            aimnames = aimdir.GetValueNames();
            foreach (string aimKey in aimnames)
            {
                if (aimKey == name)
                {
                    aimdir.DeleteValue(name);
                    DeleteOk = true;
                }
            }
            return DeleteOk;
        }
        //判断注册表数据信息是否存在
        public bool IsRegeditExist(string name)
        {
            bool isExist = false;
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey(RegeditPath, true);
            RegistryKey aimdir = software.OpenSubKey("Run");
            subkeyNames = aimdir.GetValueNames();
            foreach (string keyName in subkeyNames)
            {
                if (keyName == name)
                {
                    isExist = true;
                }
            }
            return isExist;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            string R_startPath = Application.ExecutablePath;
            if (checkBox1.Checked == true)
            {
                RegistryKey R_local = Registry.LocalMachine;
                RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                R_run.SetValue("BirthdayTipF", R_startPath);
                R_run.Close();
                R_local.Close();
                //GetLogin();
            }
            else
            {
                try
                {
                    RegistryKey R_local = Registry.LocalMachine;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue("BirthdayTipF", false);
                    R_run.Close();
                    R_local.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("您需要管理员权限修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    throw;
                }
            }
        }

        #endregion