/// <summary>
        /// Attempts to set a registry value for the key provided using the specified
        /// name, data and kind. If the registry value does not exist it will be created
        /// </summary>
        /// <param name="key">The key of which the value is to be set for.</param>
        /// <param name="name">The name of the value.</param>
        /// <param name="data">The data of the value</param>
        /// <param name="kind">The value kind of the value</param>
        /// <returns>Returns a boolean value if the action succeeded or failed.</returns>
        public static bool SetValueSafe(this RegistryKey key, string name, object data, RegistryValueKind kind)
        {
            try
            {
                // handle type conversion
                if (kind != RegistryValueKind.Binary && data.GetType() == typeof(byte[]))
                {
                    switch (kind)
                    {
                    case RegistryValueKind.String:
                    case RegistryValueKind.ExpandString:
                        data = ByteConverterHelper.ToString((byte[])data);
                        break;

                    case RegistryValueKind.DWord:
                        data = ByteConverterHelper.ToUInt32((byte[])data);
                        break;

                    case RegistryValueKind.QWord:
                        data = ByteConverterHelper.ToUInt64((byte[])data);
                        break;

                    case RegistryValueKind.MultiString:
                        data = ByteConverterHelper.ToStringArray((byte[])data);
                        break;
                    }
                }
                key.SetValue(name, data, kind);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #2
0
        public static string RegistryValueToString(RegValueData value)
        {
            switch (value.Kind)
            {
            case RegistryValueKind.Binary:
                return(value.Data.Length > 0 ? BitConverter.ToString(value.Data).Replace("-", " ").ToLower() : "(zero-length binary value)");

            case RegistryValueKind.MultiString:
                return(string.Join(" ", ByteConverterHelper.ToStringArray(value.Data)));

            case RegistryValueKind.DWord:
                var dword = ByteConverterHelper.ToUInt32(value.Data);
                return($"0x{dword:x8} ({dword})");    // show hexadecimal and decimal

            case RegistryValueKind.QWord:
                var qword = ByteConverterHelper.ToUInt64(value.Data);
                return($"0x{qword:x8} ({qword})");    // show hexadecimal and decimal

            case RegistryValueKind.String:
            case RegistryValueKind.ExpandString:
                return(ByteConverterHelper.ToString(value.Data));

            default:
                return(string.Empty);
            }
        }
        public RegValueEditMultiStringForm(RegValueData value)
        {
            _value = value;

            InitializeComponent();

            this.valueNameTxtBox.Text = value.Name;
            this.valueDataTxtBox.Text = string.Join("\r\n", ByteConverterHelper.ToStringArray(value.Data));
        }