Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a registry value exists using C#?

Tags:

c#

registry

How to check if a registry value exists by 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); } 
like image 762
sari k Avatar asked Nov 25 '10 10:11

sari k


1 Answers

For Registry Key you can check if it is null after getting it. It will be, if it doesn't exist.

For Registry Value you can get names of Values for the current key and check if this array contains the needed Value name.

Example:

public static bool checkMachineType() {         RegistryKey winLogonKey = Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\services\pcmcia", true);     return (winLogonKey.GetValueNames().Contains("Start")); } 
like image 131
26071986 Avatar answered Oct 06 '22 12:10

26071986