时间:2021-05-20
usingMicrosoft.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;
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#操作windows注册表的方法。分享给大家供大家参考。具体如下:此代码演示了如何读取和写入注册表读取注册表:privatestringGetR
本文讲解了在C#程序中设置代理和取消代理的方法,原理很简单,就是通过调用C#操作注册表的相关类来对注册表的相关内容进行修改而实现的,具体源代码如下:1、启动代理
本文实例讲述了C#实现读取注册表监控当前操作系统已安装软件变化的方法。分享给大家供大家参考。具体实现方法如下:privatestaticHybridDictio
本文实例讲述了C#通过windows注册表获取软件清单的方法。分享给大家供大家参考。具体如下:foreach(stringSoftwareNameinObjec
非Web程序(桌面程序)的设置一般都存在注册表中。给这些程序做自动化测试时,需要经常要跟注册表打交道。通过修改注册表来修改程序的设置。本章介绍如何利用C#程序如