common/CordonelAssemblyLine/Model/ExeConfig.cs
2026-04-23 17:50:07 +02:00

116 lines
2.9 KiB
C#

namespace CordonelAssemblyLine.Model
{
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
public class ExeConfig
{
public static readonly string DllFilePath
= typeof(ExeConfig).Assembly.Location;
public static readonly ExeConfig Current = new ExeConfig();
public int PickingSlot
{
get
{
_ = int.TryParse(GetValue(), out var value);
return value;
}
set => SetValue(value);
}
public string PickingBarcode
{
get => GetValue();
set => SetValue(value);
}
public int LeakSlot
{
get
{
_ = int.TryParse(GetValue(), out var value);
return value;
}
set => SetValue(value);
}
public int LeakStationId
{
get
{
_ = int.TryParse(GetValue(), out var value);
return value;
}
set => SetValue(value);
}
public string LeakTester
{
get => GetValue();
set => SetValue(value);
}
public int MarriageSlot
{
get
{
_ = int.TryParse(GetValue(), out var value);
return value;
}
set => SetValue(value);
}
public string MarriageBarcode
{
get => GetValue();
set => SetValue(value);
}
private static string GetValue([CallerMemberName] string key = "")
{
if (string.IsNullOrWhiteSpace(key))
{
return default(string);
}
var configuration = ConfigurationManager.OpenExeConfiguration(DllFilePath);
var settings = configuration?.AppSettings?.Settings ?? new KeyValueConfigurationCollection();
if (!settings.AllKeys.Contains(key))
{
return default(string);
}
return settings[key].Value;
}
private static void SetValue(object value, [CallerMemberName] string key = "")
{
if (string.IsNullOrWhiteSpace(key))
{
return;
}
var dll = typeof(ExeConfig).Assembly.Location;
var configuration = ConfigurationManager.OpenExeConfiguration(DllFilePath);
var settings = configuration?.AppSettings?.Settings ?? new KeyValueConfigurationCollection();
if (!settings.AllKeys.Contains(key))
{
settings.Add(key, string.Empty);
}
settings[key].Value = $"{value}";
configuration.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}