/// <summary>
        /// Makes sure that the key/value passed in are added to the registry
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void KeySetValue(string key, RegKeyValueChoices value = RegKeyValueChoices.Off, UInt16 OtherValue = 0)
        {
            bool foundkey = false;
            bool RegKeyNull = false;

            using (RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(RegistryLocation, true)) {
                if (RegKey != null) {
                    string[] values = RegKey.GetValueNames();
                    RegKeyNull = false;

                    if (RegKey.ValueCount > 0) {

                        foreach (string item in values) {
                            if (item == key) { //key found
                                foundkey = true;
                                if (OtherValue != 0)
                                    RegKey.SetValue(key, unchecked((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                                else
                                    RegKey.SetValue(key, value, RegistryValueKind.DWord);
                            }
                        }
                        if (foundkey != true) { //Key not found
                            if (OtherValue != 0)
                                RegKey.SetValue(key, unchecked((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                            else
                                RegKey.SetValue(key, value, RegistryValueKind.DWord);
                        }

                    }
                    else { //Executes if RegKey.ValueCount <= 0
                        if (OtherValue != 0) {
                            RegKey.SetValue(key, unchecked((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                        }
                        else {
                            RegKey.SetValue(key, value, RegistryValueKind.DWord);
                        }
                    }
                }
                else {
                    RegKeyNull = true;
                }
            }

            if (RegKeyNull) {
                using (RegistryKey RegKey = Registry.LocalMachine.CreateSubKey
                     (RegistryLocation, RegistryKeyPermissionCheck.ReadWriteSubTree)) {
                    if (OtherValue != 0) {
                        RegKey.SetValue(key, unchecked((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                    }
                    else {
                        RegKey.SetValue(key, value, RegistryValueKind.DWord);
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Makes sure that the key/value passed in are added to the registry
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void KeySetValue(string key, RegKeyValueChoices value = RegKeyValueChoices.Off, UInt16 OtherValue = 0)
        {
            bool foundkey   = false;
            bool RegKeyNull = false;

            using (RegistryKey RegKey = Registry.LocalMachine.OpenSubKey(RegistryLocation, true)) {
                if (RegKey != null)
                {
                    string[] values = RegKey.GetValueNames();
                    RegKeyNull = false;

                    if (RegKey.ValueCount > 0)
                    {
                        foreach (string item in values)
                        {
                            if (item == key)   //key found
                            {
                                foundkey = true;
                                if (OtherValue != 0)
                                {
                                    RegKey.SetValue(key, unchecked ((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                                }
                                else
                                {
                                    RegKey.SetValue(key, value, RegistryValueKind.DWord);
                                }
                            }
                        }
                        if (foundkey != true)   //Key not found
                        {
                            if (OtherValue != 0)
                            {
                                RegKey.SetValue(key, unchecked ((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                            }
                            else
                            {
                                RegKey.SetValue(key, value, RegistryValueKind.DWord);
                            }
                        }
                    }
                    else   //Executes if RegKey.ValueCount <= 0
                    {
                        if (OtherValue != 0)
                        {
                            RegKey.SetValue(key, unchecked ((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                        }
                        else
                        {
                            RegKey.SetValue(key, value, RegistryValueKind.DWord);
                        }
                    }
                }
                else
                {
                    RegKeyNull = true;
                }
            }

            if (RegKeyNull)
            {
                using (RegistryKey RegKey = Registry.LocalMachine.CreateSubKey
                                                (RegistryLocation, RegistryKeyPermissionCheck.ReadWriteSubTree)) {
                    if (OtherValue != 0)
                    {
                        RegKey.SetValue(key, unchecked ((int)Convert.ToInt32(OtherValue)), RegistryValueKind.DWord);
                    }
                    else
                    {
                        RegKey.SetValue(key, value, RegistryValueKind.DWord);
                    }
                }
            }
        }