refactor: 项目稳定性优化
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user