Files
laatzen/Common/Ui/GenesisToolBox/Infrastructure/Appsettings.cs
T
2024-02-09 08:49:32 +01:00

88 lines
2.7 KiB
C#

namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
{
using System;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
internal static class Appsettings
{
private const string EMPTY = "";
private const string SETTINGS_SECTION = "appSettings";
private static Configuration configuration
= ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
private static KeyValueConfigurationCollection settings
=> configuration.AppSettings.Settings;
public static short GMHPort
{
get => GetInt16();
set => SetValue(value);
}
public static int TemperatureChecksSec
{
get => GetInt32();
set => SetValue(value);
}
public static int MessurementStopHours
{
get => GetInt32();
set => SetValue(value);
}
public static Double MessurementStepMs { get; internal set; }
public static Double MessurementDauerMs { get; internal set; }
public static Double BackwardsStepMs { get; internal set; }
public static Int32 MessurementDauer { get; internal set; }
public static Int32 MessurementStep { get; internal set; }
private static short GetInt16([CallerMemberName] string property = EMPTY)
{
var value = settings[property]?.Value?.ToString() ?? string.Empty;
if (short.TryParse(value, out short result))
{
return result;
}
return default(short);
}
private static int GetInt32([CallerMemberName] string property = EMPTY)
{
var value = settings[property]?.Value?.ToString() ?? string.Empty;
if (int.TryParse(value, out int result))
{
return result;
}
return default(short);
}
private static void SetValue<T>(this T value, [CallerMemberName] string property = EMPTY)
{
var key = settings.AllKeys.FirstOrDefault(x => x.Equals(property, StringComparison.OrdinalIgnoreCase));
if (string.IsNullOrWhiteSpace(key))
{
key = property;
settings.Add(key, string.Empty);
}
settings[key].Value = value?.ToString() ?? EMPTY;
}
internal static void SaveSettings()
{
configuration.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(SETTINGS_SECTION);
configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
}
}
}