Changeset 14 for Common/Registry.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- 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;6 1 using Microsoft.Win32; 7 2 … … 17 12 if ((string)value == "True") return true; 18 13 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; 19 26 } 20 27 else return defaultValue; … … 35 42 } 36 43 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 37 52 public static long GetValueLong(this RegistryKey regKey, string name, long defaultValue) 38 53 { 39 54 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; 41 56 else return defaultValue; 42 57 } … … 54 69 public static void SetValueBool(this RegistryKey regKey, string name, bool value) 55 70 { 56 regKey.SetValue(name, value); 71 if (value) regKey.SetValue(name, "True"); 72 else regKey.SetValue(name, "False"); 57 73 } 58 74 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 59 84 public static void SetValueString(this RegistryKey regKey, string name, string value) 60 85 { … … 66 91 { 67 92 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, ""); 68 101 } 69 102
Note:
See TracChangeset
for help on using the changeset viewer.