| 1 | using Microsoft.Win32;
|
|---|
| 2 |
|
|---|
| 3 | namespace Common
|
|---|
| 4 | {
|
|---|
| 5 | public static class RegistryExtension
|
|---|
| 6 | {
|
|---|
| 7 | public static bool GetValueBool(this RegistryKey regKey, string name, bool defaultValue)
|
|---|
| 8 | {
|
|---|
| 9 | object value = regKey.GetValue(name, defaultValue);
|
|---|
| 10 | if (value is string)
|
|---|
| 11 | {
|
|---|
| 12 | if ((string)value == "True") return true;
|
|---|
| 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;
|
|---|
| 26 | }
|
|---|
| 27 | else return defaultValue;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | public static string GetValueString(this RegistryKey regKey, string name, string defaultValue)
|
|---|
| 31 | {
|
|---|
| 32 | object value = regKey.GetValue(name, defaultValue);
|
|---|
| 33 | if (value is string) return (string)value;
|
|---|
| 34 | else return defaultValue;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | public static int GetValueInt(this RegistryKey regKey, string name, int defaultValue)
|
|---|
| 38 | {
|
|---|
| 39 | object value = regKey.GetValue(name, defaultValue);
|
|---|
| 40 | if (value is int) return (int)value;
|
|---|
| 41 | else return defaultValue;
|
|---|
| 42 | }
|
|---|
| 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 |
|
|---|
| 52 | public static long GetValueLong(this RegistryKey regKey, string name, long defaultValue)
|
|---|
| 53 | {
|
|---|
| 54 | object value = regKey.GetValue(name, defaultValue);
|
|---|
| 55 | if (value is string && long.TryParse(value.ToString(), out long longValue)) return longValue;
|
|---|
| 56 | else return defaultValue;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | public static float GetValueFloat(this RegistryKey regKey, string name, float defaultValue)
|
|---|
| 60 | {
|
|---|
| 61 | object value = regKey.GetValue(name, defaultValue.ToString());
|
|---|
| 62 | if (float.TryParse((string) value, out float num))
|
|---|
| 63 | {
|
|---|
| 64 | return num;
|
|---|
| 65 | }
|
|---|
| 66 | else return defaultValue;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | public static double GetValueDouble(this RegistryKey regKey, string name, double defaultValue)
|
|---|
| 70 | {
|
|---|
| 71 | object value = regKey.GetValue(name, defaultValue.ToString());
|
|---|
| 72 | if (double.TryParse((string)value, out double num))
|
|---|
| 73 | {
|
|---|
| 74 | return num;
|
|---|
| 75 | }
|
|---|
| 76 | else return defaultValue;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public static double? GetValueDoubleNullable(this RegistryKey regKey, string name, double? defaultValue)
|
|---|
| 80 | {
|
|---|
| 81 | object value = regKey.GetValue(name, defaultValue.ToString());
|
|---|
| 82 | if (double.TryParse((string)value, out double num))
|
|---|
| 83 | {
|
|---|
| 84 | return num;
|
|---|
| 85 | }
|
|---|
| 86 | else if (value is string && (string)value == "") return null;
|
|---|
| 87 | else return defaultValue;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public static void SetValueBool(this RegistryKey regKey, string name, bool value)
|
|---|
| 91 | {
|
|---|
| 92 | if (value) regKey.SetValue(name, "True");
|
|---|
| 93 | else regKey.SetValue(name, "False");
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | public static void SetValueBoolNullable(this RegistryKey regKey, string name, bool? value)
|
|---|
| 97 | {
|
|---|
| 98 | if (value != null)
|
|---|
| 99 | {
|
|---|
| 100 | if (value.Value) regKey.SetValue(name, "True");
|
|---|
| 101 | else regKey.SetValue(name, "False");
|
|---|
| 102 | } else regKey.SetValue(name, "");
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | public static void SetValueString(this RegistryKey regKey, string name, string value)
|
|---|
| 106 | {
|
|---|
| 107 | if (value == null) value = "";
|
|---|
| 108 | regKey.SetValue(name, value);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | public static void SetValueInt(this RegistryKey regKey, string name, int value)
|
|---|
| 112 | {
|
|---|
| 113 | regKey.SetValue(name, value);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public static void SetValueIntNullable(this RegistryKey regKey, string name, int? value)
|
|---|
| 117 | {
|
|---|
| 118 | if (value != null)
|
|---|
| 119 | {
|
|---|
| 120 | regKey.SetValue(name, value.Value);
|
|---|
| 121 | } else regKey.SetValue(name, "");
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | public static void SetValueLong(this RegistryKey regKey, string name, long value)
|
|---|
| 125 | {
|
|---|
| 126 | regKey.SetValue(name, value);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | public static void SetValueFloat(this RegistryKey regKey, string name, float value)
|
|---|
| 130 | {
|
|---|
| 131 | regKey.SetValue(name, value);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | public static void SetValueDouble(this RegistryKey regKey, string name, double value)
|
|---|
| 135 | {
|
|---|
| 136 | regKey.SetValue(name, value);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | public static void SetValueDoubleNullable(this RegistryKey regKey, string name, double? value)
|
|---|
| 140 | {
|
|---|
| 141 | if (value != null)
|
|---|
| 142 | {
|
|---|
| 143 | regKey.SetValue(name, value);
|
|---|
| 144 | }
|
|---|
| 145 | else regKey.SetValue(name, "");
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|