public T GetValue <T>(string KeyName, string SubKey, T DefaultValue) { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Getting {0} value '{1}' in subkey '{2}', default: '{3}'", typeof(T).Name, KeyName, SubKey, DefaultValue.ToString())); } if (typeof(T) == typeof(bool)) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValue <T>(KeyName, SubKey, DefaultValue); registryValue = DefaultValue.ToString(); } bool RetVal = Convert.ToBoolean(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if (typeof(T) == typeof(string)) { string RetVal; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } RetVal = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + RetVal); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } RetVal = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + RetVal); } } if (RetVal == null) { SetValue <T>(KeyName, SubKey, DefaultValue); RetVal = DefaultValue.ToString(); } if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if (typeof(T) == typeof(decimal)) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValue <T>(KeyName, SubKey, DefaultValue); registryValue = DefaultValue.ToString(); } decimal RetVal = Convert.ToDecimal(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if ((typeof(T) == typeof(Int32)) | (typeof(T) == typeof(int))) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValue <T>(KeyName, SubKey, DefaultValue); registryValue = DefaultValue.ToString(); } Int32 RetVal = Convert.ToInt32(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } throw new DriverException("GetValue: Unknown type: " + typeof(T).Name); }
public static dynamic GetSetting(string Name, object DefaultValue = null, string SubKey = "") { //regkey is built from CompanyName\ProductName\MajorVersion.MinorVersion Version AN = Assembly.GetExecutingAssembly().GetName().Version; string Cname = System.Windows.Forms.Application.CompanyName; string Pname = System.Windows.Forms.Application.ProductName; string version = AN.Major + "." + AN.Minor; object RetVal = DefaultValue; string SKey = ""; if (!string.IsNullOrWhiteSpace(SubKey)) { SKey = "\\" + SubKey.Trim(); } try { string RKey = $"Software\\{Cname}\\{Pname}\\{version}{SKey}"; using (RegistryKey reg = Registry.CurrentUser.OpenSubKey(RKey, false)) if (reg != null) { bool Found = false; string[] Values = reg.GetValueNames(); foreach (string valu in Values) { if (valu.ToLower() == Name.ToLower()) { Found = true; RetVal = reg.GetValue(Name, DefaultValue); break; } } if (Found) { if (reg.GetValueKind(Name) == RegistryValueKind.MultiString) { if (DefaultValue is List <string> ) { RetVal = ((string[])RetVal).ToList(); } else if (DefaultValue is object[]) { RetVal = (string[])RetVal; } else if (DefaultValue is string[]) { RetVal = (string[])RetVal; } } else if (RetVal is string && DefaultValue is Point) { //{X=965,Y=399} int X = GetNumberInt(Global.GetWordBetween(RetVal.ToString(), "X=", ",")); int Y = GetNumberInt(Global.GetWordBetween(RetVal.ToString(), "Y=", "}")); RetVal = new Point(X, Y); } else if (RetVal is string && DefaultValue is Size) { //{Width=931, Height=592} int Wid = GetNumberInt(Global.GetWordBetween(RetVal.ToString(), "Width=", ",")); int Hei = GetNumberInt(Global.GetWordBetween(RetVal.ToString(), "Height=", "}")); RetVal = new Size(Wid, Hei); } else if (DefaultValue != null) { RetVal = Convert.ChangeType(RetVal, DefaultValue.GetType()); } //Else // RetVal = Convert.ChangeType(RetVal, DefaultValue.GetType) } } } catch (Exception ex) { Global.Log($"Error: {Global.ExMsg(ex)}"); } return(RetVal); }
public T GetValue <T>(string KeyName, string SubKey, T DefaultValue) { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Getting {0} value '{1}' in subkey '{2}', default: '{3}'", typeof(T).Name, KeyName, SubKey, DefaultValue.ToString())); } if (typeof(T) == typeof(bool)) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValueInvariant <T>(KeyName, SubKey, DefaultValue); bool defaultValue = Convert.ToBoolean(DefaultValue); registryValue = defaultValue.ToString(CultureInfo.InvariantCulture); } bool RetVal = Convert.ToBoolean(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if (typeof(T) == typeof(string)) { string RetVal; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } RetVal = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + RetVal); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } RetVal = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + RetVal); } } if (RetVal == null) { SetValue <T>(KeyName, SubKey, DefaultValue); RetVal = DefaultValue.ToString(); } if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if (typeof(T) == typeof(decimal)) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValueInvariant <T>(KeyName, SubKey, DefaultValue); decimal defaultValue = Convert.ToDecimal(DefaultValue); registryValue = defaultValue.ToString(CultureInfo.InvariantCulture); } decimal RetVal = Convert.ToDecimal(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } if (typeof(T) == typeof(DateTime)) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValueInvariant <T>(KeyName, SubKey, DefaultValue); return(DefaultValue); } if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue DateTime", $"String value prior to Convert: {registryValue}"); } if (DateTime.TryParse(registryValue, CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out DateTime RetVal)) { // The string parsed OK so return the parsed value; if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue DateTime", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } else // If the string fails to parse, overwrite with the default value and return this { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue DateTime", $"Failed to parse registry value, persisting and returning the default value: {DefaultValue}"); } SetValueInvariant <T>(KeyName, SubKey, DefaultValue); return(DefaultValue); } } if ((typeof(T) == typeof(Int32)) | (typeof(T) == typeof(int))) { string registryValue; if (SubKey == "") { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey is empty so getting value directly"); } registryValue = (string)baseRegistryKey.GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } else { if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "SubKey has a value so using it..."); } registryValue = (string)baseRegistryKey.CreateSubKey(SubKey).GetValue(KeyName); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", "Value retrieved OK: " + registryValue); } } if (registryValue == null) { SetValueInvariant <T>(KeyName, SubKey, DefaultValue); int defaultValue = Convert.ToInt32(DefaultValue); registryValue = defaultValue.ToString(CultureInfo.InvariantCulture); } int RetVal = Convert.ToInt32(registryValue, CultureInfo.InvariantCulture); if (LOG_CONFIGURATION_CALLS) { ServerForm.LogMessage(0, 0, 0, "GetValue", string.Format("Retrieved {0} = {1}", KeyName, RetVal.ToString())); } return((T)((object)RetVal)); } throw new DriverException("GetValue: Unknown type: " + typeof(T).Name); }