C#的注册表项读取和写入方式
答案:1 悬赏:0 手机版
解决时间 2021-11-26 12:14
- 提问者网友:贪了杯
- 2021-11-25 21:39
C#的注册表项读取和写入方式
最佳答案
- 五星知识达人网友:雪起风沙痕
- 2021-11-25 21:46
.读取指定名称的注册表的值
程序代码
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;
}
程序代码
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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯