C#操作注册表的方法

时间:2023-06-24 14:34:46 

using Microsoft.Win32 ;  

1.读取指定名称的注册表的值  


private string GetRegistData(string name)
{
string registData;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
registData = aimdir.GetValue(name).ToString();
return registData;
}


以上是读取的注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下的XXX目录中名称为name的注册表值; 

2.向注册表中写数据  


private void WTRegedit(string name,string tovalue)
{
RegistryKey hklm = Registry.LocalMachine;
RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.CreateSubKey("XXX");
aimdir.SetValue(name,tovalue);
}



以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项; 

3.删除注册表中指定的注册表项  


private void DeleteRegist(string name)
{
string[] aimnames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
aimnames = aimdir.GetSubKeyNames();
foreach(string aimKey in aimnames)
{
if(aimKey == name)
aimdir.DeleteSubKeyTree(name);
}
}



以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中删除名称为name注册表项; 

4.判断指定注册表项是否存在  


private bool IsRegeditExit(string name)
{
bool _exit = false;
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
RegistryKey aimdir = software.OpenSubKey("XXX",true);
subkeyNames = aimdir.GetSubKeyNames();
foreach(string keyName in subkeyNames)
{
if(keyName == name)
{
_exit = true;
return _exit;
}
}
return _exit;
}



以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断; 


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

namespace RegeditManager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //创建并写入
        private void button1_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine;
            try
            {
                RegistryKey software = key.CreateSubKey("software\\LabManager");
                software = key.OpenSubKey("software\\LabManager", true);
                software.SetValue("Address", @"C:\Program Files\实验室管理系统\dbcom.xml");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        //读取
        private void button2_Click(object sender, EventArgs e)
        {
            string info = string.Empty;
            RegistryKey key=Registry.LocalMachine;
            try
            {
                key = key.OpenSubKey("software\\LabManager");
                if (IsRegeditKeyExit("software\\LabManager", "Address"))
                {
                    info = key.GetValue("Address").ToString();
                    MessageBox.Show("注册表里的信息为:" + info);
                }
                else
                {
                    MessageBox.Show("键值Address不存在;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            RegistryKey key = Registry.LocalMachine;
            try
            {
                key = key.OpenSubKey("software\\LabManager",true);
                if (IsRegeditKeyExit("software\\LabManager", "Address"))
                {
                    key.DeleteValue("Address");
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("键值Address不存在;");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                key.Close();
            }
        }

        /// <summary>
        /// 判断键值是否存在
        /// </summary>
        /// <param name="RegistryStr">注册表项</param>
        /// <param name="KeyStr">键值</param>
        /// <returns></returns>
        private bool IsRegeditKeyExit(string RegistryStr,string KeyStr)
        {
            string[] subkeyNames;

            RegistryKey hkml = Registry.LocalMachine;

            RegistryKey software = hkml.OpenSubKey(RegistryStr);

            subkeyNames = software.GetValueNames();

            foreach (string keyName in subkeyNames)
            {
                if (keyName == KeyStr)  //判断键值的名称
                {
                    hkml.Close();

                    return true;
                }
            }
            hkml.Close();

            return false;
        }

        /// <summary>
        /// 判断注册表项是否存在
        /// </summary>
        /// <param name="RegistryName">例如:SOFTWARE</param>
        /// <param name="ValueStr">例如:LabManager</param>
        /// <returns></returns>
        private bool IsRegeditItemExist(string RegistryName,string ValueStr)
        {
            string[] subkeyNames;

            RegistryKey hkml = Registry.LocalMachine;

            RegistryKey software = hkml.OpenSubKey(RegistryName);

            subkeyNames = software.GetSubKeyNames();

            //取得该项下所有子项的名称的序列,并传递给预定的数组中

            foreach (string keyName in subkeyNames)  //遍历整个数组
            {
                if (keyName == ValueStr) //判断子项的名称
                {
                    hkml.Close();
                    return true;
                }
            }
            hkml.Close();
            return false;

        }
    }
}

标签:C#,注册表
0
投稿

猜你喜欢

  • IDEA设置背景为自定义照片的操作方法

    2022-12-28 09:13:08
  • c# 单例模式的实现

    2023-06-12 01:16:44
  • 在Framework 4.0中:找出新增的方法与新增的类(一)

    2021-07-07 05:26:07
  • Android实现录音静音降噪

    2023-01-06 23:46:25
  • 详解java调用python的几种用法(看这篇就够了)

    2023-04-10 22:15:57
  • Java处理Webp图片格式转换的示例代码

    2022-03-09 06:27:29
  • 浅谈Java多进程程序的运行模式

    2021-07-06 11:06:49
  • c#可空类型的作用说明

    2021-12-24 18:00:49
  • java开发BeanUtils类解决实体对象间赋值

    2022-08-25 06:31:25
  • Android如何在原生App中嵌入Flutter

    2022-03-11 17:06:06
  • springboot整合solr的方法详解

    2023-01-20 17:54:37
  • 一步步实现Viewpager卡片翻页效果

    2022-10-15 02:14:25
  • SpringBoot对Druid配置SQL监控功能失效问题及解决方法

    2023-06-10 21:31:24
  • Android批量修改文件格式/文件名的神操作分享

    2022-07-13 00:31:53
  • IntelliJ IDEA 中使用jRebel进行 Java 热部署教程图解

    2023-04-20 19:23:42
  • java解析XML Node与Element的区别(推荐)

    2021-11-12 18:32:13
  • 编写简易Android天气应用的代码示例

    2023-10-16 18:30:36
  • Android使用httpPost向服务器发送请求的方法

    2021-12-08 19:37:07
  • 使用Java实现三种等级的扫雷游戏(完整版)

    2023-05-10 07:34:17
  • C#中的时间显示格式(12小时制VS24小时制)

    2021-10-19 12:58:41
  • asp之家 软件编程 m.aspxhome.com