Changeset 14 for Common/Registry.cs


Ignore:
Timestamp:
Aug 2, 2022, 11:46:25 AM (22 months ago)
Author:
chronos
Message:
  • Modified: Various improvements.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/Registry.cs

    r13 r14  
    1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Threading.Tasks;
    61using Microsoft.Win32;
    72
     
    1712                if ((string)value == "True") return true;
    1813                else return false;
     14            }
     15            else return defaultValue;
     16        }
     17
     18        public static bool? GetValueBoolNullable(this RegistryKey regKey, string name, bool? defaultValue)
     19        {
     20            object value = regKey.GetValue(name, defaultValue);
     21            if (value is string)
     22            {
     23                if ((string)value == "False") return false;
     24                else if ((string)value == "True") return true;
     25                else return null;
    1926            }
    2027            else return defaultValue;
     
    3542        }
    3643
     44        public static int? GetValueIntNullable(this RegistryKey regKey, string name, int? defaultValue)
     45        {
     46            object value = regKey.GetValue(name, defaultValue);
     47            if (value is int) return (int)value;
     48            else if (value is string && (string)value == "") return null;
     49            else return defaultValue;
     50        }
     51
    3752        public static long GetValueLong(this RegistryKey regKey, string name, long defaultValue)
    3853        {
    3954            object value = regKey.GetValue(name, defaultValue);
    40             if (value is long) return (long)value;
     55            if (value is string && long.TryParse(value.ToString(), out long longValue)) return longValue;
    4156            else return defaultValue;
    4257        }
     
    5469        public static void SetValueBool(this RegistryKey regKey, string name, bool value)
    5570        {
    56             regKey.SetValue(name, value);
     71            if (value) regKey.SetValue(name, "True");
     72                else regKey.SetValue(name, "False");
    5773        }
    5874
     75        public static void SetValueBoolNullable(this RegistryKey regKey, string name, bool? value)
     76        {
     77            if (value != null)
     78            {
     79                if (value.Value) regKey.SetValue(name, "True");
     80                else regKey.SetValue(name, "False");
     81            } else regKey.SetValue(name, "");
     82        }
     83   
    5984        public static void SetValueString(this RegistryKey regKey, string name, string value)
    6085        {
     
    6691        {
    6792            regKey.SetValue(name, value);
     93        }
     94
     95        public static void SetValueIntNullable(this RegistryKey regKey, string name, int? value)
     96        {
     97            if (value != null)
     98            {
     99                regKey.SetValue(name, value.Value);
     100            } else regKey.SetValue(name, "");
    68101        }
    69102
Note: See TracChangeset for help on using the changeset viewer.