C#操作注册表——读、写、删除、判断等基本操作 (转)
2021-04-25 06:26
标签:using close cal returns eval each 添加 技术分享 setvalue 一、引入命名空间: 二、创建注册表项:CreateSubKey(name)方法 添加SubKey时候首先要打开一个表项,并设置参数为true,才能成功创建子项 三、删除注册表项:DeleteSubKey(name,true)方法 四、添加键值:SetValue(name,value)方法 五、删除键值:DeleteValue(name)方法 六、判断注册表项是否存在: 七、判断键值是否存在: 注:如果没有该注册表项或键值,则返回的值为空值,若不经处理运行程序会抛出异常。 C#操作注册表——读、写、删除、判断等基本操作 (转) 标签:using close cal returns eval each 添加 技术分享 setvalue 原文地址:http://www.cnblogs.com/candyzhmm/p/7922079.htmlusing Microsoft.Win32;
//给要创建的注册表设置一个变量,使用更方便
private string RegistFileName = "test";///
//SOFTWARE在LocalMachine分支下
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE",true);
RegistryKey software = key.CreateSubKey(RegistFileName);
RegistryKey test = key.OpenSubKey(RegistFileName,true);
RegistryKey Savekey = test.CreateSubKey("SaveLog");
}RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey("SOFTWARE", true);
RegistryKey test = software.OpenSubKey(RegistFileName, true);
test.DeleteSubKey("SaveLog",true);
///
RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey("SOFTWARE", true);
RegistryKey test = software.OpenSubKey(RegistFileName, true);
RegistryKey OpenPath = test.OpenSubKey("OpenLog",true);
//"name"是该键值的name,相当于一个别名,可自行设置
OpenPath.SetValue("name", path);
}RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.OpenSubKey("SOFTWARE", true);
RegistryKey test = software.OpenSubKey(RegistFileName, true);
RegistryKey OpenPath = test.OpenSubKey("SaveLog",true);
//删除SaveLog子项下的名为SaveLog的子键
OpenPath.DeleteValue("name");///
///
//获取该子项下的所有键值的名称saveSubkeyNames
saveSubkeyNames = Savekey.GetSubKeyNames();
foreach(string keyName in openSubkeyNames)
{
if(keyName == name)
{
key.Close();
return false;
}
}
key.Close();
return true;
}