首页 > 编程语言 >C# 操作注册表Helper类

C# 操作注册表Helper类

时间:2023-09-08 19:56:13浏览次数:37  
标签:rkt RegistryKey string Helper C# 基项 注册表 rk

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

namespace Controls.Helper
{
        public class RegistryHelper
        {
        /// <summary>
        /// 默认注册表基项
        /// </summary>
        private string baseKey = "Software";

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseKey">基项的名称</param>
        public RegistryHelper()
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseKey">基项的名称</param>
        public RegistryHelper(string baseKey)
        {
            this.baseKey = baseKey;

        }
        #endregion


        #region 公共方法
        /// <summary>
        /// 写入注册表,如果指定项已经存在,则修改指定项的值
        /// </summary>
        /// <param name="keytype">注册表基项枚举</param>
        /// <param name="key">注册表项,不包括基项</param>
        /// <param name="name">值名称</param>
        /// <param name="values">值</param>
        public void SetValue(KeyType keytype, string key, string name, string values)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey, true);
            RegistryKey rkt = software.CreateSubKey(key);
            if (rkt != null)
            {
                rkt.SetValue(name, values);
            }
        }


        
        /// <summary>
        /// 读取注册表
        /// </summary>
        /// <param name="keytype">注册表基项枚举</param>
        /// <param name="key">注册表项,不包括基项</param>
        /// <param name="name">值名称</param>
        /// <returns>返回字符串</returns>
        public string GetValue(KeyType keytype, string key, string name)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey, true);
            RegistryKey rkt = software.OpenSubKey(key);

            if (rkt != null)
            {
                return rkt.GetValue(name)?.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 删除注册表中的值
        /// </summary>
        /// <param name="keytype">注册表基项枚举</param>
        /// <param name="key">注册表项名称,不包括基项</param>
        /// <param name="name">值名称</param>
        public void DeleteValue(KeyType keytype, string key, string name)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey, true);
            RegistryKey rkt = software.OpenSubKey(key, true);
            
            if (rkt != null)
            {
                object value = rkt.GetValue(name);
                if (value != null)
                {
                    rkt.DeleteValue(name, true);
                }
            }
        }

        /// <summary>
        /// 删除注册表中的指定项
        /// </summary>
        /// <param name="keytype">注册表基项枚举</param>
        /// <param name="key">注册表中的项,不包括基项</param>
        /// <returns>返回布尔值,指定操作是否成功</returns>
        public void DeleteSubKey(KeyType keytype, string key)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey, true);
            if (software != null)
            {
                software.DeleteSubKeyTree(key);
            }
        }

        /// <summary>
        /// 判断指定项是否存在
        /// </summary>
        /// <param name="keytype">基项枚举</param>
        /// <param name="key">指定项字符串</param>
        /// <returns>返回布尔值,说明指定项是否存在</returns>
        public bool IsExist(KeyType keytype, string key)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey);
            RegistryKey rkt = software.OpenSubKey(key);
            if (rkt != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 检索指定项关联的所有值
        /// </summary>
        /// <param name="keytype">基项枚举</param>
        /// <param name="key">指定项字符串</param>
        /// <returns>返回指定项关联的所有值的字符串数组</returns>
        public string[] GetValues(KeyType keytype, string key)
        {
            RegistryKey rk = (RegistryKey)GetRegistryKey(keytype);
            RegistryKey software = rk.OpenSubKey(baseKey, true);
            RegistryKey rkt = software.OpenSubKey(key);
            string[] names = rkt.GetValueNames();
            if (names.Length == 0)
            {
                return names;
            }
            else
            {
                string[] values = new string[names.Length];

                int i = 0;

                foreach (string name in names)
                {
                    values[i] = rkt.GetValue(name).ToString();

                    i++;
                }

                return values;
            }

        }

        /// <summary>
        /// 将对象所有属性写入指定注册表中
        /// </summary>
        /// <param name="keytype">注册表基项枚举</param>
        /// <param name="key">注册表项,不包括基项</param>
        /// <param name="obj">传入的对象</param>
        public void SetObjectValue(KeyType keyType, string key, Object obj)
        {
            if (obj != null)
            {
                Type t = obj.GetType();

                string name;
                object value;
                foreach (var p in t.GetProperties())
                {
                    if (p != null)
                    {
                        name = p.Name;
                        value = p.GetValue(obj, null);
                        this.SetValue(keyType, key, name, value.ToString());
                    }
                }
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 返回RegistryKey对象
        /// </summary>
        /// <param name="keyType">注册表基项枚举</param>
        /// <returns></returns>
        private object GetRegistryKey(KeyType keyType)
        {
            RegistryKey rk = null;
            
            switch (keyType)
            {
                case KeyType.HKEY_CLASS_ROOT:
                    rk = Registry.ClassesRoot;
                    break;
                case KeyType.HKEY_CURRENT_USER:
                    rk = Registry.CurrentUser;
                    break;
                case KeyType.HKEY_LOCAL_MACHINE:
                    rk = Registry.LocalMachine;
                    break;
                case KeyType.HKEY_USERS:
                    rk = Registry.Users;
                    break;
                case KeyType.HKEY_CURRENT_CONFIG:
                    rk = Registry.CurrentConfig;
                    break;
            }

            return rk;
        }

        #endregion

        #region 枚举
        /// <summary>
        /// 注册表基项枚举
        /// </summary>
        public enum KeyType : int
        {
            /// <summary>
            /// 注册表基项 HKEY_CLASSES_ROOT
            /// </summary>
            HKEY_CLASS_ROOT,
            /// <summary>
            /// 注册表基项 HKEY_CURRENT_USER
            /// </summary>
            HKEY_CURRENT_USER,
            /// <summary>
            /// 注册表基项 HKEY_LOCAL_MACHINE
            /// </summary>
            HKEY_LOCAL_MACHINE,
            /// <summary>
            /// 注册表基项 HKEY_USERS
            /// </summary>
            HKEY_USERS,
            /// <summary>
            /// 注册表基项 HKEY_CURRENT_CONFIG
            /// </summary>
            HKEY_CURRENT_CONFIG
        }
        #endregion
    }
}

  

标签:rkt,RegistryKey,string,Helper,C#,基项,注册表,rk
From: https://www.cnblogs.com/jnyyq/p/17688441.html

相关文章

  • AI 编程助手 Amazon CodeWhisperer 使用(持续更新中)
    文章作者:天博近期chatGPT非常火爆,试用一些公众号功能,及自己接入的chatGPT,写小作文能力一流,但是准确率很低,一直在搜寻可以在idea中使用,且非常丝滑的AI编程工具,前不久看到infoq推荐的AmazonCodeWhisperer,最近一周一直试用,感觉很赞,所以强烈推荐亚马逊云科技开发者社......
  • ES6 Module模块,在vsCode中已服务器模式运行HTML文件
    操作步骤如下:一、安装LiveServer插件二、点击扩展设置三、设置liveserver默认打开浏览器为“chrome”四、配置-工作区五、在HTML文件中,右键选择"openwithLiveServer" 输出结果 ......
  • c#多进程开发
    介绍相比多线程开发,C#的多进程开发相对麻烦,无法像多线程一样对某个函数直接启动。只能将函数打包成exe文件,再使用process启动exe文件并传参,传入的参数必须序列化为字符串。过程1、构建执行函数因为要将数据分进程处理,所以构建了一个函数,输入参数对象bundleMerge,执行函数即可......
  • Alembic:Python数据库迁移工具
    Alembic是一款轻量型的数据库迁移工具,它与SQLAlchemy一起共同为Python提供数据库管理与迁移支持。Alembic的应用Alembic使用SQLAlchemy作为数据库引擎,为关系型数据提供创建、管理、更改和调用的管理脚本,协助开发和运维人员在系统上线后对数据库进行在线管理。同任何P......
  • [AGC058D] Yet Another ABC String
    [AGC058D]YetAnotherABCStringAtcoder:[AGC058D]YetAnotherABCString洛谷:[AGC058D]YetAnotherABCStringProblem给出\(a,b,c\),求由\(a\)个A,\(b\)个B,\(c\)个C构成的字符串数量,使得不存在子串ABC,BCA和CAB。\(1\leqa,b,c\leq10^6\)。Solution可能是......
  • 【题解】CF1854D Michael and Hotel
    交互题。考虑题意即为找到\(1\)所在内向基环树上的所有点。我们考虑我们怎么找到环上的点,我们考虑我们可以\(O(\logn)\)询问到一个环上的点,方法即为将\(k\)定为一个大数,然后二分点集。然后我们便可以在\(O(n\logn)\)的时间复杂度内找到所有环上的点(我们一会儿再讲怎......
  • C# 写入类型到文件Helper类
    usingSystem;usingSystem.IO;usingSystem.Text;namespaceControls.Helper{publicclassLoggerHelper{privatestaticobjecto=newobject();privatestaticstringGetLogFilePath(){stringfilePath=AppD......
  • C++多线程编程:包括多线程打印ABC、线程池实现等等
    #include<iostream>#include<thread>#include<mutex>#include<condition_variable>std::condition_variablecond;std::mutexprint_mutex;intflag=0;voidprint_thread(intnum){for(inti=0;i<10;i++)//循环{......
  • 【230908-16】▲ABC中,a=2,c=二倍根号2,C=45°,则S△ABC=?
    ......
  • 【230908-17】▲ABC中,b=2,B=30°,C=45°,则S△ABC=?(2013年全国II卷)
    ......