113 lines
3.0 KiB
C#
113 lines
3.0 KiB
C#
namespace Xylem.Common.Ui.GenesisToolBox.Infrastructure
|
|
{
|
|
using System;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
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 String WorkingDirectory
|
|
{
|
|
get
|
|
{
|
|
try
|
|
{
|
|
return Directory
|
|
.GetParent(configuration.FilePath)
|
|
.FullName;
|
|
}
|
|
catch
|
|
{
|
|
return ".";
|
|
}
|
|
}
|
|
}
|
|
|
|
public static Int16 GMHPort
|
|
{
|
|
get => GetInt16();
|
|
set => SetValue(value);
|
|
}
|
|
|
|
|
|
public static Int32 MessurementDauer
|
|
{
|
|
get => GetInt32();
|
|
set => SetValue(value);
|
|
}
|
|
|
|
public static Int32 MessurementStep
|
|
{
|
|
get => GetInt32();
|
|
set => SetValue(value);
|
|
}
|
|
|
|
public static String GetKey(String key)
|
|
{
|
|
var _key = settings.AllKeys.FirstOrDefault(x => x.Equals(key, StringComparison.OrdinalIgnoreCase));
|
|
|
|
if (string.IsNullOrWhiteSpace(_key))
|
|
{
|
|
_key = key;
|
|
|
|
settings.Add(_key, string.Empty);
|
|
}
|
|
|
|
return _key;
|
|
}
|
|
|
|
private static String GetValue([CallerMemberName] String property = EMPTY)
|
|
=> settings[property]?.Value?.ToString() ?? string.Empty;
|
|
|
|
private static Int16 GetInt16([CallerMemberName] String property = EMPTY)
|
|
{
|
|
var value = GetValue(property);
|
|
|
|
if (short.TryParse(value, out var result))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return default(Int16);
|
|
}
|
|
|
|
private static Int32 GetInt32([CallerMemberName] String property = EMPTY)
|
|
{
|
|
var value = GetValue(property);
|
|
|
|
if (int.TryParse(value, out var result))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
return default(Int16);
|
|
}
|
|
|
|
private static void SetValue<T>(this T value, [CallerMemberName] String property = EMPTY)
|
|
{
|
|
var key = GetKey(property);
|
|
|
|
settings[key].Value = value?.ToString() ?? EMPTY;
|
|
}
|
|
|
|
internal static void SaveSettings()
|
|
{
|
|
configuration.Save(ConfigurationSaveMode.Full);
|
|
ConfigurationManager.RefreshSection(SETTINGS_SECTION);
|
|
|
|
configuration = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
|
|
settings = configuration.AppSettings.Settings;
|
|
}
|
|
}
|
|
}
|