c#注册表类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;


namespace Qsg
{
    ///
    /// 注册表操作类
    ///
    public class WRegisterTool
    {
        ///
        /// 获得根节点注册表
        ///
        /// 根节点类型
        ///
        public static RegistryKey GetRootRegisterKey(WRegisterRootKeyType rootKeyType)
        {
            switch (rootKeyType)
            {
                case WRegisterRootKeyType.HKEY_CLASSES_ROOT:
                    return Registry.ClassesRoot;
                case WRegisterRootKeyType.HKEY_CURRENT_CONFIG:
                    return Registry.CurrentConfig;
                case WRegisterRootKeyType.HKEY_CURRENT_USER:
                    return Registry.CurrentUser;
                case WRegisterRootKeyType.HKEY_LOCAL_MACHINE:
                    return Registry.LocalMachine;
                default:
                    throw new Exception("注册表类型未定义!");
            }
        }

        ///
        /// 在指定注册表项下创建注册表子项
        ///
        /// 父注册表项
        /// 注册表路径
        ///
        public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath)
        {
            RegistryKey returnKey = fatherKey.CreateSubKey(keyPath);
            return returnKey;
        }

        ///
        /// 在指定注册表项下创建注册表子项
        ///
        /// 父注册表项
        /// 注册表路径
        /// 要添加的注册表项值名称
        /// 要添加的注册表项值
        ///
        public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath, string keyValueName, string keyValue)
        {
            RegistryKey returnKey = CreateRegistryKey(fatherKey, keyPath);
            returnKey.SetValue(keyValueName, keyValue);
            return returnKey;
        }

        ///
        /// 在指定注册表项下创建注册表子项
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        ///
        public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath)
        {
            RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
            return CreateRegistryKey(rootKeyType, keyPath);
        }

        ///
        /// 在指定注册表项下创建注册表子项
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        /// 要添加的注册表项值名称
        /// 要添加的注册表项值
        ///
        public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue)
        {
            RegistryKey returnKey = CreateRegistryKey(rootKeyType, keyPath);
            returnKey.SetValue(keyValueName, keyValue);
            return returnKey;
        }

        ///
        /// 删除注册表子项
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        ///
        public static bool DeleteRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyName)
        {
            RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
            RegistryKey deleteKey = rootKey.OpenSubKey(keyPath);

            if (IsKeyHaveSubKey(deleteKey, keyName))
            {
                deleteKey.DeleteSubKey(keyName);
                return true;
            }
            return false;
        }

 

        ///
        /// 获取注册表项
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        ///
        public static RegistryKey GetRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath)
        {
            RegistryKey rootKey = GetRootRegisterKey(rootKeyType);
            return rootKey.OpenSubKey(keyPath);
        }

 

        ///
        /// 获取注册表项指定值
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        /// 要获得值的注册表值名称
        ///
        public static string GetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyName)
        {
            RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
            if (IsKeyHaveValue(key, keyName))
            {
                return key.GetValue(keyName).ToString();
            }
            return null;
        }

 

        ///
        /// 设置注册表项值
        ///
        /// 注册表项
        /// 值名称
        /// 值
        ///
        public static bool SetRegistryValue(RegistryKey key, string keyValueName, string keyValue)
        {
            key.SetValue(keyValueName, keyValue);
            return true;
        }

 

        ///
        /// 设置注册表项值
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        /// 值名称
        /// 值
        ///
        public static bool SetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue)
        {
            RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
            key.SetValue(keyValueName, keyValue);
            return true;
        }

 

        ///
        /// 删除注册表项值
        ///
        /// 注册表项
        /// 值名称
        /// 值
        ///
        public static bool DeleteRegistryValue(RegistryKey key, string keyValueName)
        {
            if (IsKeyHaveValue(key, keyValueName))
            {
                key.DeleteValue(keyValueName);
                return true;
            }
            return false;
        }

 

        ///
        /// 删除注册表项值
        ///
        /// 注册表根节点项类型
        /// 注册表路径
        /// 值名称
        ///
        public static bool DeleteRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName)
        {
            RegistryKey key = GetRegistryKey(rootKeyType, keyPath);
            if (IsKeyHaveValue(key, keyValueName))
            {
                key.DeleteValue(keyValueName);
                return true;
            }
            return false;
        }

 

        ///
        /// 判断注册表项是否有指定的注册表值
        ///
        /// 注册表项
        /// 注册表值
        ///
        private static bool IsKeyHaveValue(RegistryKey key, string valueName)
        {
            string[] keyNames = key.GetValueNames();
            foreach (string keyName in keyNames)
            {
                if (keyName.Trim() == valueName.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        ///
        /// 判断注册表项是否有指定的子项
        ///
        /// 注册表项
        /// 子项名称
        ///
        private static bool IsKeyHaveSubKey(RegistryKey key, string keyName)
        {
            string[] subKeyNames = key.GetSubKeyNames();
            foreach (string subKeyName in subKeyNames)
            {
                if (keyName.Trim() == keyName.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        ///
        /// 根据注册表键路径获得注册表键名称
        ///
        /// 注册表键路径
        ///
        private static string GetKeyNameByPath(string keyPath)
        {
            int keyIndex = keyPath.LastIndexOf("/");
            return keyPath.Substring(keyIndex + 1);
        }

 

 

        public enum WRegisterRootKeyType
        {
            HKEY_CLASSES_ROOT = 0,
            HKEY_CURRENT_USER = 1,
            HKEY_LOCAL_MACHINE = 2,
            HKEY_USERS = 3,
            HKEY_CURRENT_CONFIG = 4
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值