How to check if registry value exists using C #?

How to check if a registry value exists with C # code? This is my code, I want to check if "Start" exists.

public static bool checkMachineType() { RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true); string currentKey= winLogonKey.GetValue("Start").ToString(); if (currentKey == "0") return (false); return (true); } 
+66
c # registry
Nov 25 '10 at 10:44
source share
7 answers

For the registry key, you can check if it is null after receiving it. It will be so if it does not exist.

As a registry value, you can get the value names for the current key and check whether this array contains the required value name.

Example:

 public static bool checkMachineType() { RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true); return (winLogonKey.GetValueNames().Contains("Start")); } 
+52
Nov 25. '10 at
source share
 public static bool RegistryValueExists(string hive_HKLM_or_HKCU, string registryRoot, string valueName) { RegistryKey root; switch (hive_HKLM_or_HKCU.ToUpper()) { case "HKLM": root = Registry.LocalMachine.OpenSubKey(registryRoot, false); break; case "HKCU": root = Registry.CurrentUser.OpenSubKey(registryRoot, false); break; default: throw new System.InvalidOperationException("parameter registryRoot must be either \"HKLM\" or \"HKCU\""); } return root.GetValue(valueName) != null; } 
+40
Sep 10 '12 at 19:46
source share
 string keyName=@"HKEY_LOCAL_MACHINE\System\CurrentControlSet\services\pcmcia"; string valueName="Start"; if (Registry.GetValue(keyName, valueName, null) == null) { //code if key Not Exist } else { //code if key Exist } 
+23
Nov 25 '10 at 11:21
source share
  RegistryKey rkSubKey = Registry.CurrentUser.OpenSubKey(" Your Registry Key Location", false); if (rkSubKey == null) { // It doesn't exist } else { // It exists and do something if you want to } 
+2
Sep 10
source share
 public bool ValueExists(RegistryKey Key, string Value) { try { return Key.GetValue(Value) != null; } catch { return false; } } 

This simple function will return true only if the value is found, but it is not equal to zero, otherwise it will return false if the value exists, but it is zero or the value does not exist in the key.




USE for your question:

 if (ValueExists(winLogonKey, "Start") { // The values exists } else { // The values does not exists } 
0
May 13 '19 at 19:27
source share
  RegistryKey test9999 = Registry.CurrentUser; foreach (var item in test9999.GetSubKeyNames()) { if (item.ToString() == "SOFTWARE") { test9999.OpenSubKey(item); foreach (var val in test9999.OpenSubKey(item).GetSubKeyNames()) { if(val.ToString() == "Adobe") { Console.WriteLine(val+ " found it "); } } } } 
-2
Apr 30 '17 at 13:55 on
source share
  internal static Func<string, string, bool> regKey = delegate (string KeyLocation, string Value) { // get registry key with Microsoft.Win32.Registrys RegistryKey rk = (RegistryKey)Registry.GetValue(KeyLocation, Value, null); // KeyLocation and Value variables from method, null object because no default value is present. Must be casted to RegistryKey because method returns object. if ((rk) == null) // if the RegistryKey is null which means it does not exist { // the key does not exist return false; // return false because it does not exist } // the registry key does exist return true; // return true because it does exist }; 

using:

  // usage: /* Create Key - while (loading) { RegistryKey k; k = Registry.CurrentUser.CreateSubKey("stuff"); k.SetValue("value", "value"); Thread.Sleep(int.MaxValue); }; // no need to k.close because exiting control */ if (regKey(@"HKEY_CURRENT_USER\stuff ... ", "value")) { // key exists return; } // key does not exist 
-four
Sep 08 '16 at 0:26
source share



All Articles