C# 实现注册表的读取

直接上方法,至于怎么调用,偶想大家应该会的

using Microsoft.Win32;
using System;
using System.Windows.Forms;

namespace Lambda
{
    public class ModifyRegistry
    {
        private bool showError = false;
        private string subKey = "SOFTWARE\\、、、、、、、";//select where you want
        private Microsoft.Win32.RegistryKey baseRegistryKey = Microsoft.Win32.Registry.CurrentUser;
        public bool ShowError
        {
            get
            {
                return this.showError;
            }
            set
            {
                this.showError = value;
            }
        }
        public string SubKey
        {
            get
            {
                return this.subKey;
            }
            set
            {
                this.subKey = value;
            }
        }
        public Microsoft.Win32.RegistryKey BaseRegistryKey
        {
            get
            {
                return this.baseRegistryKey;
            }
            set
            {
                this.baseRegistryKey = value;
            }
        }
        public string Read(string KeyName)
        {
            Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
            Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
            string result;
            if (registryKey2 == null)
            {
                result = null;
            }
            else
            {
                try
                {
                    result = (string)registryKey2.GetValue(KeyName.ToUpper());
                }
                catch (System.Exception e)
                {
                    this.ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
                    result = null;
                }
            }
            return result;
        }
        public bool Write(string KeyName, object Value)
        {
            bool result;
            try
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.CreateSubKey(this.subKey);
                registryKey2.SetValue(KeyName.ToUpper(), Value);
                result = true;
            }
            catch (System.Exception e)
            {
                this.ShowErrorMessage(e, "Writing registry " + KeyName.ToUpper());
                result = false;
            }
            return result;
        }
        public bool DeleteKey(string KeyName)
        {
            bool result;
            try
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.CreateSubKey(this.subKey);
                if (registryKey2 == null)
                {
                    result = true;
                }
                else
                {
                    registryKey2.DeleteValue(KeyName);
                    result = true;
                }
            }
            catch (System.Exception e)
            {
                this.ShowErrorMessage(e, "Deleting SubKey " + this.subKey);
                result = false;
            }
            return result;
        }
        public bool DeleteSubKeyTree()
        {
            bool result;
            try
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                if (registryKey2 != null)
                {
                    registryKey.DeleteSubKeyTree(this.subKey);
                }
                result = true;
            }
            catch (System.Exception e)
            {
                this.ShowErrorMessage(e, "Deleting SubKey " + this.subKey);
                result = false;
            }
            return result;
        }
        public int SubKeyCount()
        {
            int result;
            try
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                if (registryKey2 != null)
                {
                    result = registryKey2.SubKeyCount;
                }
                else
                {
                    result = 0;
                }
            }
            catch (System.Exception e)
            {
                this.ShowErrorMessage(e, "Retriving subkeys of " + this.subKey);
                result = 0;
            }
            return result;
        }
        public int ValueCount()
        {
            int result;
            try
            {
                Microsoft.Win32.RegistryKey registryKey = this.baseRegistryKey;
                Microsoft.Win32.RegistryKey registryKey2 = registryKey.OpenSubKey(this.subKey);
                if (registryKey2 != null)
                {
                    result = registryKey2.ValueCount;
                }
                else
                {
                    result = 0;
                }
            }
            catch (System.Exception e)
            {
                this.ShowErrorMessage(e, "Retriving keys of " + this.subKey);
                result = 0;
            }
            return result;
        }
        private void ShowErrorMessage(System.Exception e, string Title)
        {
            if (this.showError)
            {
                MessageBox.Show(e.Message, Title, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }
    }
}

转载于:https://www.cnblogs.com/karrydong/archive/2012/12/05/2802509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值