Files
IBKRTrader/Core/Settings/SettingsService.cs
T
RichardandClaude Opus 4.8 ebeb035e92 Initial commit: IBKRTrader
.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>
2026-07-26 18:19:47 +02:00

50 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
}
}