2010-11-25 8 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

レジストリキーについては、レジストリキーの取得後にnullであるかどうかを確認できます。それが存在しなければ、それはあります。

レジストリ値の場合、現在のキーの値の名前を取得し、この配列に必要な値の名前が含まれているかどうかを確認できます。

+15

例、それは質問が求めているものであるから? – cja

+2

私はこれが受け入れられた答えだとは信じられませんo.O – lewis4u

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

+0

@benはデフォルト値を ''存在しない ''から ''ヌル ''に変更しました(これによりコードが修正されました)。 @abatishchevの前のコメントはもう適用されません。 –

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; 
} 
+0

この回答は間違いありませんが、この質問はすでに回答済みです。 – hsanders

+25

@hsanders質問が既に回答されていても、有用な情報を追加することはいつでも歓迎します。スタックオーバーフローはGoogleから多くのトラフィックを受け取ります; – DonkeyMaster

+2

root.GetValue(valueName)!= valueNameが存在しない場合はnullが例外をスローします。 – Farukh

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

この解決策は、キーが存在するかどうかをチェックすることです。値については、そのキーの値のリストをチェックする必要があります – jammy

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