是参照着 这篇文章写出来的 https://blog.csdn.net/younghaiqing/article/details/61918968

以下是写好的单例 只有基础的功能

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

namespace TestTwo
{
    class MyRegistryInstance
    {
        private static MyRegistryInstance instance;
        public static MyRegistryInstance Instance()
        {
            if (instance == null)
            {
                instance = new MyRegistryInstance();
            }
            return instance;
        }
        /// <summary>
        /// 创建注册表项
        /// </summary>
        /// <param name="itemName">创建的注册表项的name</param>
        public void creatRegistryFunc(string itemName)
        {
            //创建    创建注册表项主要用到RegistryKey 的CreateSubKey()方法
            RegistryKey key = Registry.LocalMachine;
            //RegistryKey software = key.CreateSubKey("software\\test");
            RegistryKey software = key.CreateSubKey("software\\"+itemName);
        }
        /// <summary>
        /// 打开注册表项
        /// </summary>
        /// <param name="itemName">要打开的注册表项的name</param>
        public void openRegistryFunc(string itemName)
        {
            //打开    打开注册表项主要用到RegistryKey 的OpenSubKey()方法。    注意,如果该注册表项不存在,调用这个方法会抛出异常
            RegistryKey key = Registry.LocalMachine;
            //RegistryKey software = key.OpenSubKey("software\\test", true);//注意该方法后面还可以有一个布尔型的参数,true表示可以写入。false为只读
            RegistryKey software = key.OpenSubKey("software\\"+itemName, true);
        }
        /// <summary>
        /// 删除注册表项  注意,如果该注册表项不存在,这调用这个方法会抛出异常
        /// </summary>
        /// <param name="itemName">要删除的注册表项的name</param>
        public void deleteRegistryFunc(string itemName)
        {
            //删除
            RegistryKey key = Registry.LocalMachine;
            //key.DeleteSubKey("software\\test", true);//注意,如果该注册表项不存在,这调用这个方法会抛出异常
            key.DeleteSubKey("software\\" + itemName, true);
            key.Close();

        }
        /// <summary>
        /// 键值的创建(设置值、修改)    对键值的创建修改等操作
        /// </summary>
        /// <param name="itemName">要创建键值的注册表项的name</param>
        /// <param name="keyName">要创建的键值对的name</param>
        /// <param name="keyValue">要创建的键值对的value</param>
        public void creatKeyFunc(string itemName,string keyName,string keyValue)
        {
            //键值的创建(设置值、修改)    对键值的创建修改等操作主要用到RegistryKey 的SetValue()方法
            RegistryKey key = Registry.LocalMachine;
            //RegistryKey software = key.OpenSubKey("software\\test", true);//该项必须已存在
            RegistryKey software = key.OpenSubKey("software\\"+ itemName, true);//该项必须已存在

            //在HKEY_LOCAL_MACHINE\SOFTWARE\test下创建一个名为“test”,值为“博客园”的键值。如果该键值原本已经存在,则会修改替换原来的键值,如果不存在则是创建该键值。
            //software.SetValue("test", "博客园");
            software.SetValue(keyName, keyValue);
            // 注意:SetValue()还有第三个参数,主要是用于设置键值的类型,如:字符串,二进制,Dword等等~~默认是字符串。如:
            // software.SetValue("test", "0", RegistryValueKind.DWord); //二进制信息
            key.Close();
        }
        /// <summary>
        /// 读取键值
        /// </summary>
        /// <param name="itemName">要读取键值的注册表项的name</param>
        /// <param name="keyName">要读取的键值对的name</param>
        /// <returns>要读取的键值对的value</returns>
        public string readKenFunc(string itemName,string keyName)
        {
            string info = "";
            RegistryKey key;
            key = Registry.LocalMachine;
            //RegistryKey myReg = key.OpenSubKey("software\\test");
            RegistryKey myReg = key.OpenSubKey("software\\"+ itemName);
            // myReg = Key.OpenSubKey("software\\test",true);
            //info = myReg.GetValue("test").ToString();
            info = myReg.GetValue(keyName).ToString();
            myReg.Close();
            return info;
        }
        /// <summary>
        /// 删除键值
        /// </summary>
        /// <param name="itemName">要删除键值的注册表项的name</param>
        /// <param name="keyName">要删除的键值对的name</param>
        public void deleteKeyFunc(string itemName,string keyName)
        {
            //删除注册表项Software\\test中的键名为test的值
            /*RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true);
            delKey.DeleteValue("test");*/
            RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\"+ itemName, true);
            delKey.DeleteValue(keyName);
            delKey.Close();
        }
        /// <summary>
        /// 判断注册表项是否存在
        /// </summary>
        /// <param name="itemName">要判断的注册表项的name</param>
        /// <returns>boo 是则为true 否则为false</returns>
        public bool IsRegeditItemExist(string itemName)
        {
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            RegistryKey software = hkml.OpenSubKey("SOFTWARE");
            //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true); 
            subkeyNames = software.GetSubKeyNames();//获取该项下所有子项的名称的序列,并传递给预定的数组中

            /*foreach (string keyName in subkeyNames)
            {
                //判断子项的名称
                if (keyName == "test")
                {
                    hkml.Close();
                    return true;
                }
            }*/
            foreach (string _itemName in subkeyNames)
            {
                //判断子项的名称
                if (_itemName == itemName)
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
        /// <summary>
        /// 判断键值是否存在
        /// </summary>
        /// <param name="itemName">要判断键值的注册表项的name</param>
        /// <param name="keyName">要判断的键值对的name</param>
        /// <returns>boo 是则为true 否则为false</returns>
        public bool IsRegeditKeyExit(string itemName,string keyName)
        {
            string[] subkeyNames;
            RegistryKey hkml = Registry.LocalMachine;
            //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test");
            RegistryKey software = hkml.OpenSubKey("SOFTWARE\\"+ itemName);
            //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
            subkeyNames = software.GetValueNames();//取得该项下所有键值的名称的序列,并传递给预定的数组中

            /*foreach (string keyName in subkeyNames)
            {
                if (keyName == "test")
                {
                    hkml.Close();
                    return true;
                }
            }*/
            foreach (string _keyName in subkeyNames)
            {
                if (_keyName == keyName)
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;
        }
    }
}


 

posted on 2018-04-29 16:09  凌落成迷  阅读(630)  评论(0编辑  收藏  举报