add:配置文件损坏自动恢复
This commit is contained in:
@@ -0,0 +1,216 @@
|
|||||||
|
using Ink_Canvas.Helpers;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Ink_Canvas.Helpers
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 自动备份管理器
|
||||||
|
/// 负责管理配置文件的自动备份功能
|
||||||
|
/// </summary>
|
||||||
|
public static class AutoBackupManager
|
||||||
|
{
|
||||||
|
private static readonly string BackupDir = Path.Combine(App.RootPath, "Backups");
|
||||||
|
private static readonly string SettingsFile = Path.Combine(App.RootPath, "Configs", "Settings.json");
|
||||||
|
private static readonly string BackupPrefix = "Settings_AutoBackup_";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查是否需要执行自动备份
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">设置对象</param>
|
||||||
|
/// <returns>如果需要备份返回true,否则返回false</returns>
|
||||||
|
public static bool ShouldPerformAutoBackup(Settings settings)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 如果自动备份功能未启用,不执行备份
|
||||||
|
if (!settings.Advanced.IsAutoBackupEnabled)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果从未备份过,需要创建首次备份
|
||||||
|
if (settings.Advanced.LastAutoBackupTime == DateTime.MinValue)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已超过备份间隔
|
||||||
|
var daysSinceLastBackup = (DateTime.Now - settings.Advanced.LastAutoBackupTime).TotalDays;
|
||||||
|
return daysSinceLastBackup >= settings.Advanced.AutoBackupIntervalDays;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"检查自动备份条件时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行自动备份
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">设置对象</param>
|
||||||
|
/// <returns>备份是否成功</returns>
|
||||||
|
public static bool PerformAutoBackup(Settings settings)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 确保备份目录存在
|
||||||
|
if (!Directory.Exists(BackupDir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(BackupDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查主配置文件是否存在
|
||||||
|
if (!File.Exists(SettingsFile))
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("主配置文件不存在,跳过自动备份", LogHelper.LogType.Warning);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建备份文件名(使用当前日期时间)
|
||||||
|
string backupFileName = $"{BackupPrefix}{DateTime.Now:yyyyMMdd_HHmmss}.json";
|
||||||
|
string backupPath = Path.Combine(BackupDir, backupFileName);
|
||||||
|
|
||||||
|
// 复制主配置文件到备份位置
|
||||||
|
File.Copy(SettingsFile, backupPath, true);
|
||||||
|
|
||||||
|
// 更新最后备份时间
|
||||||
|
settings.Advanced.LastAutoBackupTime = DateTime.Now;
|
||||||
|
MainWindow.SaveSettingsToFile();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"执行自动备份时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 尝试从备份恢复配置文件
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>恢复是否成功</returns>
|
||||||
|
public static bool TryRestoreFromBackup()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 确保备份目录存在
|
||||||
|
if (!Directory.Exists(BackupDir))
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("备份目录不存在,无法从备份恢复", LogHelper.LogType.Warning);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找最新的备份文件
|
||||||
|
var backupFiles = Directory.GetFiles(BackupDir, $"{BackupPrefix}*.json")
|
||||||
|
.OrderByDescending(f => File.GetCreationTime(f))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (backupFiles.Length == 0)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("没有找到可用的备份文件", LogHelper.LogType.Warning);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用最新的备份文件
|
||||||
|
string latestBackup = backupFiles[0];
|
||||||
|
|
||||||
|
// 验证备份文件是否有效
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string backupJson = File.ReadAllText(latestBackup);
|
||||||
|
var testSettings = JsonConvert.DeserializeObject<Settings>(backupJson);
|
||||||
|
if (testSettings == null)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("备份文件内容无效,无法恢复", LogHelper.LogType.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"备份文件验证失败: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 备份当前损坏的配置文件(如果存在)
|
||||||
|
if (File.Exists(SettingsFile))
|
||||||
|
{
|
||||||
|
string corruptedBackup = Path.Combine(BackupDir, $"Settings_Corrupted_{DateTime.Now:yyyyMMdd_HHmmss}.json");
|
||||||
|
File.Copy(SettingsFile, corruptedBackup, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从备份恢复配置文件
|
||||||
|
File.Copy(latestBackup, SettingsFile, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"从备份恢复配置文件时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清理过期的备份文件
|
||||||
|
/// 保留最近30天的备份文件
|
||||||
|
/// </summary>
|
||||||
|
public static void CleanupOldBackups()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(BackupDir))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cutoffDate = DateTime.Now.AddDays(-30);
|
||||||
|
var backupFiles = Directory.GetFiles(BackupDir, $"{BackupPrefix}*.json");
|
||||||
|
|
||||||
|
int deletedCount = 0;
|
||||||
|
foreach (var file in backupFiles)
|
||||||
|
{
|
||||||
|
if (File.GetCreationTime(file) < cutoffDate)
|
||||||
|
{
|
||||||
|
File.Delete(file);
|
||||||
|
deletedCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deletedCount > 0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"清理过期备份文件时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化自动备份功能
|
||||||
|
/// 在应用程序启动时调用
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="settings">设置对象</param>
|
||||||
|
public static void Initialize(Settings settings)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// 检查是否需要执行自动备份
|
||||||
|
if (ShouldPerformAutoBackup(settings))
|
||||||
|
{
|
||||||
|
PerformAutoBackup(settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 清理过期备份
|
||||||
|
CleanupOldBackups();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"初始化自动备份功能时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2333,6 +2333,28 @@
|
|||||||
</ui:SimpleStackPanel>
|
</ui:SimpleStackPanel>
|
||||||
|
|
||||||
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
|
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
|
||||||
|
<TextBlock Foreground="#fafafa" Text="定期自动备份" VerticalAlignment="Center"
|
||||||
|
FontSize="14" Margin="0,0,16,0" />
|
||||||
|
<ui:ToggleSwitch OnContent="" OffContent="" Name="ToggleSwitchIsAutoBackupEnabled"
|
||||||
|
IsOn="True" FontFamily="Microsoft YaHei UI" FontWeight="Bold"
|
||||||
|
Toggled="ToggleSwitchIsAutoBackupEnabled_Toggled" />
|
||||||
|
</ui:SimpleStackPanel>
|
||||||
|
|
||||||
|
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
|
||||||
|
<TextBlock Foreground="#fafafa" Text="备份间隔" VerticalAlignment="Center"
|
||||||
|
FontSize="14" Margin="0,0,16,0" />
|
||||||
|
<ComboBox x:Name="ComboBoxAutoBackupInterval" Width="100" Margin="0,0,16,0"
|
||||||
|
SelectionChanged="ComboBoxAutoBackupInterval_SelectionChanged">
|
||||||
|
<ComboBoxItem Content="1天" Tag="1" />
|
||||||
|
<ComboBoxItem Content="3天" Tag="3" />
|
||||||
|
<ComboBoxItem Content="7天" Tag="7" IsSelected="True" />
|
||||||
|
<ComboBoxItem Content="14天" Tag="14" />
|
||||||
|
<ComboBoxItem Content="30天" Tag="30" />
|
||||||
|
</ComboBox>
|
||||||
|
<TextBlock Foreground="#a1a1aa" Text="(默认7天)" VerticalAlignment="Center" FontSize="12" />
|
||||||
|
</ui:SimpleStackPanel>
|
||||||
|
|
||||||
|
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,10,0,0">
|
||||||
<Button x:Name="BtnManualBackup" Content="手动备份" Click="BtnManualBackup_Click"
|
<Button x:Name="BtnManualBackup" Content="手动备份" Click="BtnManualBackup_Click"
|
||||||
Background="#2563eb" Foreground="White" Padding="12,6" Margin="0,0,12,0" />
|
Background="#2563eb" Foreground="White" Padding="12,6" Margin="0,0,12,0" />
|
||||||
<Button x:Name="BtnRestoreBackup" Content="还原备份" Click="BtnRestoreBackup_Click"
|
<Button x:Name="BtnRestoreBackup" Content="还原备份" Click="BtnRestoreBackup_Click"
|
||||||
|
|||||||
@@ -390,6 +390,8 @@ namespace Ink_Canvas
|
|||||||
loadPenCanvas();
|
loadPenCanvas();
|
||||||
//加载设置
|
//加载设置
|
||||||
LoadSettings(true);
|
LoadSettings(true);
|
||||||
|
AutoBackupManager.Initialize(Settings);
|
||||||
|
|
||||||
// 检查保存路径是否可用,不可用则修正
|
// 检查保存路径是否可用,不可用则修正
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2371,6 +2371,26 @@ namespace Ink_Canvas
|
|||||||
SaveSettingsToFile();
|
SaveSettingsToFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void ToggleSwitchIsAutoBackupEnabled_Toggled(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
if (!isLoaded) return;
|
||||||
|
Settings.Advanced.IsAutoBackupEnabled = ToggleSwitchIsAutoBackupEnabled.IsOn;
|
||||||
|
SaveSettingsToFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ComboBoxAutoBackupInterval_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||||
|
{
|
||||||
|
if (!isLoaded) return;
|
||||||
|
if (ComboBoxAutoBackupInterval.SelectedItem is ComboBoxItem selectedItem && selectedItem.Tag != null)
|
||||||
|
{
|
||||||
|
if (int.TryParse(selectedItem.Tag.ToString(), out int interval))
|
||||||
|
{
|
||||||
|
Settings.Advanced.AutoBackupIntervalDays = interval;
|
||||||
|
SaveSettingsToFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void BtnManualBackup_Click(object sender, RoutedEventArgs e)
|
private void BtnManualBackup_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (!isLoaded) return;
|
if (!isLoaded) return;
|
||||||
|
|||||||
@@ -28,12 +28,86 @@ namespace Ink_Canvas
|
|||||||
{
|
{
|
||||||
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
||||||
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
|
|
||||||
|
// 验证设置是否成功加载
|
||||||
|
if (Settings == null)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("配置文件解析失败,尝试从备份恢复", LogHelper.LogType.Warning);
|
||||||
|
if (AutoBackupManager.TryRestoreFromBackup())
|
||||||
|
{
|
||||||
|
// 重新尝试加载
|
||||||
|
text = File.ReadAllText(App.RootPath + settingsFileName);
|
||||||
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
|
if (Settings != null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果仍然失败,使用默认设置
|
||||||
|
if (Settings == null)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("从备份恢复失败,使用默认设置", LogHelper.LogType.Warning);
|
||||||
|
BtnResetToSuggestion_Click(null, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"配置文件加载失败: {ex.Message}", LogHelper.LogType.Error);
|
||||||
|
|
||||||
|
// 尝试从备份恢复
|
||||||
|
LogHelper.WriteLogToFile("尝试从备份恢复配置文件", LogHelper.LogType.Warning);
|
||||||
|
if (AutoBackupManager.TryRestoreFromBackup())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
||||||
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
|
if (Settings != null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception restoreEx)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"从备份恢复后重新加载失败: {restoreEx.Message}", LogHelper.LogType.Error);
|
||||||
|
Settings = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果仍然失败,使用默认设置
|
||||||
|
if (Settings == null)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("从备份恢复失败,使用默认设置", LogHelper.LogType.Warning);
|
||||||
|
BtnResetToSuggestion_Click(null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
BtnResetToSuggestion_Click(null, null);
|
LogHelper.WriteLogToFile("配置文件不存在,尝试从备份恢复", LogHelper.LogType.Warning);
|
||||||
|
if (AutoBackupManager.TryRestoreFromBackup())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string text = File.ReadAllText(App.RootPath + settingsFileName);
|
||||||
|
Settings = JsonConvert.DeserializeObject<Settings>(text);
|
||||||
|
if (Settings != null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception restoreEx)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile($"从备份恢复后加载失败: {restoreEx.Message}", LogHelper.LogType.Error);
|
||||||
|
Settings = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果仍然失败,使用默认设置
|
||||||
|
if (Settings == null)
|
||||||
|
{
|
||||||
|
LogHelper.WriteLogToFile("从备份恢复失败,使用默认设置", LogHelper.LogType.Warning);
|
||||||
|
BtnResetToSuggestion_Click(null, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -714,6 +788,17 @@ namespace Ink_Canvas
|
|||||||
ToggleSwitchIsEnableDPIChangeDetection.IsOn = Settings.Advanced.IsEnableDPIChangeDetection;
|
ToggleSwitchIsEnableDPIChangeDetection.IsOn = Settings.Advanced.IsEnableDPIChangeDetection;
|
||||||
ToggleSwitchIsEnableAvoidFullScreenHelper.IsOn = Settings.Advanced.IsEnableAvoidFullScreenHelper;
|
ToggleSwitchIsEnableAvoidFullScreenHelper.IsOn = Settings.Advanced.IsEnableAvoidFullScreenHelper;
|
||||||
ToggleSwitchIsAutoBackupBeforeUpdate.IsOn = Settings.Advanced.IsAutoBackupBeforeUpdate;
|
ToggleSwitchIsAutoBackupBeforeUpdate.IsOn = Settings.Advanced.IsAutoBackupBeforeUpdate;
|
||||||
|
ToggleSwitchIsAutoBackupEnabled.IsOn = Settings.Advanced.IsAutoBackupEnabled;
|
||||||
|
|
||||||
|
// 设置备份间隔下拉框
|
||||||
|
foreach (ComboBoxItem item in ComboBoxAutoBackupInterval.Items)
|
||||||
|
{
|
||||||
|
if (item.Tag != null && int.TryParse(item.Tag.ToString(), out int interval) && interval == Settings.Advanced.AutoBackupIntervalDays)
|
||||||
|
{
|
||||||
|
ComboBoxAutoBackupInterval.SelectedItem = item;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (Settings.Advanced.IsEnableFullScreenHelper)
|
if (Settings.Advanced.IsEnableFullScreenHelper)
|
||||||
{
|
{
|
||||||
FullScreenHelper.MarkFullscreenWindowTaskbarList(new WindowInteropHelper(this).Handle, true);
|
FullScreenHelper.MarkFullscreenWindowTaskbarList(new WindowInteropHelper(this).Handle, true);
|
||||||
|
|||||||
@@ -575,6 +575,15 @@ namespace Ink_Canvas
|
|||||||
[JsonProperty("isAutoBackupBeforeUpdate")]
|
[JsonProperty("isAutoBackupBeforeUpdate")]
|
||||||
public bool IsAutoBackupBeforeUpdate { get; set; } = true;
|
public bool IsAutoBackupBeforeUpdate { get; set; } = true;
|
||||||
|
|
||||||
|
[JsonProperty("isAutoBackupEnabled")]
|
||||||
|
public bool IsAutoBackupEnabled { get; set; } = true;
|
||||||
|
|
||||||
|
[JsonProperty("autoBackupIntervalDays")]
|
||||||
|
public int AutoBackupIntervalDays { get; set; } = 7;
|
||||||
|
|
||||||
|
[JsonProperty("lastAutoBackupTime")]
|
||||||
|
public DateTime LastAutoBackupTime { get; set; } = DateTime.MinValue;
|
||||||
|
|
||||||
[JsonProperty("isNoFocusMode")]
|
[JsonProperty("isNoFocusMode")]
|
||||||
public bool IsNoFocusMode { get; set; } = true;
|
public bool IsNoFocusMode { get; set; } = true;
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user