2010-11-25 94 views
56

如何通過C#代碼檢查註冊表值是否存在? 這是我的代碼,我想檢查'開始'是否存在。如何使用C#檢查註冊表值是否存在?

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); 
} 

回答

44

對於註冊表鍵,您可以檢查它是否爲空後得到它。這將是,如果它不存在。

對於註冊表值,您可以獲取當前鍵的值的名稱,並檢查該數組是否包含所需的值名稱。

+15

後者的例子,因爲這是什麼的問題是問? – cja 2014-04-10 14:48:58

+2

我不能相信這是被接受的答案o.O – lewis4u 2017-10-28 12:47:58

20
string [email protected]"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 
} 
+11

如果鍵的值是「不存在」?這意味着`defaultValue`不適用於關鍵存在檢查,這是爲了避免額外的空檢查。 – abatishchev 2010-11-25 11:23:33

+0

@ben將默認值從「不存在」改爲「null」(從而修復代碼)。因此,以前來自@abatishchev的評論不再適用。 – 2015-10-05 13:59:17

35
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; 
} 
3
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 
     } 
-1
 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 
     }; 

用法:

 // 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 
0
 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 "); 
        } 
       } 
      } 
     }