refactor: 项目稳定性优化

This commit is contained in:
2026-01-17 17:00:15 +08:00
parent 7abd445039
commit c276e9e2b9
7 changed files with 135 additions and 110 deletions

View File

@@ -11,16 +11,26 @@ namespace TimerApp
public int IdleThresholdSeconds { get; set; } = 30;
public bool IsDarkMode { get; set; } = true;
private static string ConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
private static string LegacyConfigPath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
private static string ConfigPath
{
get
{
string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TimerApp");
return Path.Combine(dir, "settings.json");
}
}
public static AppSettings Load()
{
try
{
if (File.Exists(ConfigPath))
string path = File.Exists(ConfigPath) ? ConfigPath : LegacyConfigPath;
if (File.Exists(path))
{
string json = File.ReadAllText(ConfigPath);
return JsonSerializer.Deserialize<AppSettings>(json);
string json = File.ReadAllText(path);
return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
}
}
catch
@@ -34,6 +44,7 @@ namespace TimerApp
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(ConfigPath)!);
string json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(ConfigPath, json);
}