Import des bestehenden Projektstands in Git. - .NET 10 WinForms Anwendung (Multi-Agent / Tool-System) - .gitignore fuer Build-Artefakte, Secrets und Runtime-Daten ergaenzt Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
69 lines
1.9 KiB
C#
69 lines
1.9 KiB
C#
using System.Text.Json;
|
||
using ClawdDotNet.Models;
|
||
|
||
namespace ClawdDotNet.Services;
|
||
|
||
public sealed class SettingsManager
|
||
{
|
||
private const string SettingsFileName = "Settings.json";
|
||
|
||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||
{
|
||
WriteIndented = true,
|
||
ReadCommentHandling = JsonCommentHandling.Skip,
|
||
AllowTrailingCommas = true,
|
||
PropertyNameCaseInsensitive = true
|
||
};
|
||
|
||
private readonly string _settingsPath;
|
||
|
||
public AppSettings AppSettings { get; private set; } = new();
|
||
|
||
public SettingsManager(string? basePath = null)
|
||
{
|
||
var dir = basePath ?? AppDomain.CurrentDomain.BaseDirectory;
|
||
_settingsPath = Path.Combine(dir, SettingsFileName);
|
||
}
|
||
|
||
public void Load()
|
||
{
|
||
if (!File.Exists(_settingsPath))
|
||
{
|
||
AppSettings = new AppSettings();
|
||
Save(); // Defaults schreiben
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
var json = File.ReadAllText(_settingsPath);
|
||
AppSettings = JsonSerializer.Deserialize<AppSettings>(json, JsonOptions)
|
||
?? new AppSettings();
|
||
}
|
||
catch
|
||
{
|
||
AppSettings = new AppSettings();
|
||
}
|
||
}
|
||
|
||
public void Save()
|
||
{
|
||
try
|
||
{
|
||
var dir = Path.GetDirectoryName(_settingsPath);
|
||
if (!string.IsNullOrEmpty(dir))
|
||
Directory.CreateDirectory(dir);
|
||
|
||
var json = JsonSerializer.Serialize(AppSettings, JsonOptions);
|
||
File.WriteAllText(_settingsPath, json);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
// Logging ist hier ggf. noch nicht verfügbar – Fallback auf MessageBox
|
||
MessageBox.Show(
|
||
$"Settings konnten nicht gespeichert werden:\n{ex.Message}",
|
||
"Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
}
|