Files
ClawdDotNet/Services/SettingsManager.cs
T
RichardandClaude Opus 4.8 2fed388c99 Initial commit: ClawdDotNet
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>
2026-07-26 18:21:46 +02:00

69 lines
1.9 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 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);
}
}
}