.NET WinForms-Anwendung (Core, Modules/CongressTrading, UI). Enthaelt .gitignore und settings.example.json als Konfigurationsvorlage. Echte settings.json mit Zugangsdaten ist bewusst ausgeschlossen. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
50 lines
1.3 KiB
C#
50 lines
1.3 KiB
C#
using System.Text.Json;
|
||
using System.Text.Json.Serialization;
|
||
|
||
namespace IBKRTrader.Core.Settings;
|
||
|
||
/// <summary>
|
||
/// Lädt und speichert settings.json neben der EXE.
|
||
/// Singleton – wird beim App-Start einmalig geladen.
|
||
/// </summary>
|
||
public class SettingsService
|
||
{
|
||
private static readonly string SettingsPath =
|
||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
|
||
|
||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||
{
|
||
WriteIndented = true,
|
||
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
|
||
PropertyNameCaseInsensitive = true
|
||
};
|
||
|
||
public AppSettings Settings { get; private set; } = new();
|
||
|
||
public void Load()
|
||
{
|
||
if (!File.Exists(SettingsPath))
|
||
{
|
||
Save(); // Defaults auf Disk schreiben
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
var json = File.ReadAllText(SettingsPath);
|
||
Settings = JsonSerializer.Deserialize<AppSettings>(json, JsonOptions) ?? new AppSettings();
|
||
}
|
||
catch
|
||
{
|
||
Settings = new AppSettings();
|
||
Save();
|
||
}
|
||
}
|
||
|
||
public void Save()
|
||
{
|
||
var json = JsonSerializer.Serialize(Settings, JsonOptions);
|
||
File.WriteAllText(SettingsPath, json);
|
||
}
|
||
}
|