C#操作注册表的方法

四爷灬man

四爷灬man

2016-02-19 11:57

今天图老师小编要跟大家分享C#操作注册表的方法,精心挑选的过程简单易学,喜欢的朋友一起来学习吧!
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_MACHINESOFTWARE目录下的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_MACHINESOFTWARE目录下新建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_MACHINESOFTWARE目录下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_MACHINESOFTWARE目录下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();

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

            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();

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

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

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

        }
    }
}

展开更多 50%)
分享

猜你喜欢

C#操作注册表的方法

编程语言 网络编程
C#操作注册表的方法

C#中对注册表的操作

电脑网络
C#中对注册表的操作

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

c#下注册表操作的一个小细节

编程语言 网络编程
c#下注册表操作的一个小细节

使用Java操作Windows注册表

编程语言 网络编程
使用Java操作Windows注册表

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

注册表优化方法

电脑入门
注册表优化方法

VC++ 中对注册表的操作

编程语言 网络编程
VC++ 中对注册表的操作

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

JAVA专题技术综述之线程篇

JAVA专题技术综述之线程篇

HTML教程:收集的常用的HTML标签(4)

HTML教程:收集的常用的HTML标签(4)
下拉加载更多内容 ↓