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
Coomphs Sep 08 '16 at 0:26 2016-09-08 00:26
source share