时间:2022-06-27 08:49:46 | 栏目:.NET代码 | 点击:次
Registry类,RegistryKey类提供了操作注册表的接口
RegistryValueKind:用于指定操作注册表的数据类型
在注册表中,最上面的节点是注册表巢(registry hive)。
Registry类封装了注册表的七个基本主键:
const string KeyName = "HKEY_CURRENT_USER\\Example"; Registry.SetValue(keyName, "", 5280);//默认名称 Registry.SetValue(keyName, "TestLong", 1234567, RegistryValueKind.QWord); int i = (int)Registry.GetValue(keyName, "", -1);//默认值 long l = (long)Registry.GetValue(keyName, "TestLong", long.MinValue);
RegistryKey类封装了对注册表的基本操作。包括读、写、删等操作的常用函数:
//使用CreateSubKey()在SOFTWARE下创建子项test RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.CreateSubKey(@"SOFTWARE\test"); hklm.Close(); hkSoftWare.Close();
//使用OpenSubKey()打开项,获得RegistryKey对象,当路径不存在时,为Null。第二个参数为true,表示可写,可读,可删;省略时只能读。 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true); hklm.Close(); hkSoftWare.Close();
//主要用到了DeleteSubKey(),删除test项 RegistryKey hklm = Registry.LocalMachine; hklm.DeleteSubKey(@"SOFTWARE\test", true); //为true时,删除的注册表不存在时抛出异常;当为false时不抛出异常。 hklm.Close();
//主要用到了SetValue(),表示在test下创建名称为Name,值为RegistryTest的键值。第三个参数表示键值类型,省略时,默认为字符串 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test",true); hkSoftWare.SetValue("Name", "RegistryTest", RegistryValueKind.String); hklm.Close(); hkSoftWare.Close();
//主要用到了GetValue(),获得名称为"Name"的键值 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true); string sValue = hkSoftWare.GetValue("Name").ToString(); hklm.Close(); hkSoftWare.Close();
//主要用到了DeleteValue(),表示删除名称为"Name"的键值,第二个参数表示是否抛出异常 RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE\test", true); hkSoftWare.DeleteValue("Name", true); hklm.Close(); hkSoftWare.Close();
//判断注册表项是否存在 private bool IsRegistryKeyExist(string sKeyName) { string[] sKeyNameColl; RegistryKey hklm = Registry.LocalMachine; RegistryKey hkSoftWare = hklm.OpenSubKey(@"SOFTWARE"); sKeyNameColl = hkSoftWare.GetSubKeyNames(); //获取SOFTWARE下所有的子项 foreach (string sName in sKeyNameColl) { if (sName == sKeyName) { hklm.Close(); hkSoftWare.Close(); return true; } } hklm.Close(); hkSoftWare.Close(); return false; } //判断键值是否存在 private bool IsRegistryValueNameExist(string sValueName) { string[] sValueNameColl; RegistryKey hklm = Registry.LocalMachine; RegistryKey hkTest = hklm.OpenSubKey(@"SOFTWARE\test"); sValueNameColl = hkTest.GetValueNames(); //获取test下所有键值的名称 foreach (string sName in sValueNameColl) { if (sName == sValueName) { hklm.Close(); hkTest.Close(); return true; } } hklm.Close(); hkTest.Close(); return false; }
//开启程序自启动 string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.SetValue("JcShutdown", path); rk2.Close(); rk.Close(); //关闭程序自启动 string path = Application.ExecutablePath; RegistryKey rk = Registry.LocalMachine; RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); rk2.DeleteValue("JcShutdown", false); rk2.Close(); rk.Close();
RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine,"192.168.0.2"); RegistryKey softwareKey = key.OpenSubKey("software\\test"); softwareKey.Close(); baseKey.Close();