| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- using System.IO;
- using System.Text.Json;
- using SlamCross.Models;
- namespace SlamCross.Services;
- public class SettingsService
- {
- private static readonly string SettingsPath = Path.Combine(
- Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
- "SLAM", "settings.json");
- public AppSettings Settings { get; private set; } = new();
- public void Load()
- {
- try
- {
- if (File.Exists(SettingsPath))
- {
- var json = File.ReadAllText(SettingsPath);
- Settings = JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
- }
- }
- catch
- {
- Settings = new AppSettings();
- }
- }
- public void Save()
- {
- try
- {
- Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath)!);
- var json = JsonSerializer.Serialize(Settings, new JsonSerializerOptions { WriteIndented = true });
- File.WriteAllText(SettingsPath, json);
- }
- catch { }
- }
- }
|