add:备份功能

This commit is contained in:
2025-07-16 15:28:36 +08:00
parent 1e96477127
commit 285f211f50
5 changed files with 158 additions and 0 deletions
+35
View File
@@ -11,6 +11,7 @@ using System.Windows.Controls;
using System.IO.Compression;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Ink_Canvas.Helpers
{
@@ -417,6 +418,40 @@ namespace Ink_Canvas.Helpers
{
try
{
// 在更新前备份设置文件
try
{
// 检查是否开启了自动备份
if (MainWindow.Settings.Advanced.IsAutoBackupBeforeUpdate)
{
// 确保Backups目录存在
string backupDir = Path.Combine(App.RootPath, "Backups");
if (!Directory.Exists(backupDir))
{
Directory.CreateDirectory(backupDir);
LogHelper.WriteLogToFile($"创建备份目录: {backupDir}", LogHelper.LogType.Info);
}
// 创建备份文件名(使用当前日期时间和版本号)
string backupFileName = $"Settings_BeforeUpdate_v{version}_{DateTime.Now:yyyyMMdd_HHmmss}.json";
string backupPath = Path.Combine(backupDir, backupFileName);
// 序列化当前设置并保存到备份文件
string settingsJson = Newtonsoft.Json.JsonConvert.SerializeObject(MainWindow.Settings, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(backupPath, settingsJson);
LogHelper.WriteLogToFile($"更新前自动备份设置成功: {backupPath}", LogHelper.LogType.Info);
}
else
{
LogHelper.WriteLogToFile("更新前自动备份功能已禁用,跳过备份", LogHelper.LogType.Info);
}
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"更新前自动备份设置时出错: {ex.Message}", LogHelper.LogType.Error);
}
string zipFilePath = Path.Combine(updatesFolderPath, $"InkCanvasForClass.CE.{version}.zip");
LogHelper.WriteLogToFile($"AutoUpdate | Checking for ZIP file: {zipFilePath}");
+23
View File
@@ -1877,6 +1877,29 @@
</ui:SimpleStackPanel>
<TextBlock TextWrapping="Wrap" Foreground="#a1a1aa"
Text="# 当检测到系统分辨率变化时,会尝试检测FloatingBar是否在屏幕内显示,如果不在屏幕内显示将会尝试移动到屏幕内可见区域(分辨率调小可能会触发,如果在屏幕内不会自动调整位置,请手动挡)。" />
<!-- 添加备份相关按钮 -->
<Line HorizontalAlignment="Center" X1="0" Y1="0" X2="400" Y2="0" Stroke="#3f3f46"
StrokeThickness="1" Margin="0,4,0,4" />
<TextBlock Text="设置备份与还原" FontWeight="Bold" Foreground="#fafafa"
FontSize="16" Margin="0,5,0,5" />
<TextBlock TextWrapping="Wrap" Foreground="#a1a1aa"
Text="# 可手动备份当前设置或还原之前的备份,自动更新前也会自动备份" />
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBlock Foreground="#fafafa" Text="自动更新前备份" VerticalAlignment="Center"
FontSize="14" Margin="0,0,16,0" />
<ui:ToggleSwitch OnContent="" OffContent="" Name="ToggleSwitchIsAutoBackupBeforeUpdate"
IsOn="True" FontFamily="Microsoft YaHei UI" FontWeight="Bold"
Toggled="ToggleSwitchIsAutoBackupBeforeUpdate_Toggled" />
</ui:SimpleStackPanel>
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0,5,0,0">
<Button x:Name="BtnManualBackup" Content="手动备份" Click="BtnManualBackup_Click"
Background="#2563eb" Foreground="White" Padding="12,6" Margin="0,0,12,0" />
<Button x:Name="BtnRestoreBackup" Content="还原备份" Click="BtnRestoreBackup_Click"
Background="#2563eb" Foreground="White" Padding="12,6" />
</ui:SimpleStackPanel>
</ui:SimpleStackPanel>
</GroupBox>
<GroupBox>
+96
View File
@@ -13,6 +13,8 @@ using System.Windows.Media.Imaging;
using System.Windows.Interop;
using Hardcodet.Wpf.TaskbarNotification;
using OSVersionExtension;
using Microsoft.Win32;
using System.IO;
namespace Ink_Canvas {
public partial class MainWindow : Window {
@@ -1813,6 +1815,100 @@ namespace Ink_Canvas {
Settings.Advanced.IsSecondConfirmWhenShutdownApp = ToggleSwitchIsSecondConfimeWhenShutdownApp.IsOn;
SaveSettingsToFile();
}
private void ToggleSwitchIsAutoBackupBeforeUpdate_Toggled(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
Settings.Advanced.IsAutoBackupBeforeUpdate = ToggleSwitchIsAutoBackupBeforeUpdate.IsOn;
SaveSettingsToFile();
}
private void BtnManualBackup_Click(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
try {
// 确保Backups目录存在
string backupDir = Path.Combine(App.RootPath, "Backups");
if (!Directory.Exists(backupDir)) {
Directory.CreateDirectory(backupDir);
LogHelper.WriteLogToFile($"创建备份目录: {backupDir}", LogHelper.LogType.Info);
}
// 创建备份文件名(使用当前日期时间)
string backupFileName = $"Settings_Backup_{DateTime.Now:yyyyMMdd_HHmmss}.json";
string backupPath = Path.Combine(backupDir, backupFileName);
// 序列化当前设置并保存到备份文件
string settingsJson = JsonConvert.SerializeObject(Settings, Formatting.Indented);
File.WriteAllText(backupPath, settingsJson);
LogHelper.WriteLogToFile($"成功创建设置备份: {backupPath}", LogHelper.LogType.Info);
MessageBox.Show($"设置已成功备份到:\n{backupPath}", "备份成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex) {
LogHelper.WriteLogToFile($"创建设置备份时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"创建备份失败: {ex.Message}", "备份失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void BtnRestoreBackup_Click(object sender, RoutedEventArgs e) {
if (!isLoaded) return;
try {
// 确保Backups目录存在
string backupDir = Path.Combine(App.RootPath, "Backups");
if (!Directory.Exists(backupDir)) {
Directory.CreateDirectory(backupDir);
LogHelper.WriteLogToFile($"创建备份目录: {backupDir}", LogHelper.LogType.Info);
MessageBox.Show("没有找到备份文件,请先创建备份", "还原失败", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
// 打开文件选择对话框
OpenFileDialog dlg = new OpenFileDialog();
dlg.InitialDirectory = backupDir;
dlg.Filter = "设置备份文件|Settings_Backup_*.json|所有JSON文件|*.json";
dlg.Title = "选择要还原的备份文件";
if (dlg.ShowDialog() == true) {
// 读取备份文件
string backupJson = File.ReadAllText(dlg.FileName);
// 反序列化备份数据
Settings backupSettings = JsonConvert.DeserializeObject<Settings>(backupJson);
if (backupSettings != null) {
// 确认是否要还原
if (MessageBox.Show("确定要还原选择的备份文件吗?当前设置将被覆盖。", "确认还原",
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) {
// 备份当前设置,以防出错
string currentSettingsJson = JsonConvert.SerializeObject(Settings, Formatting.Indented);
string tempBackupPath = Path.Combine(backupDir, $"Settings_Before_Restore_{DateTime.Now:yyyyMMdd_HHmmss}.json");
File.WriteAllText(tempBackupPath, currentSettingsJson);
// 还原设置
Settings = backupSettings;
// 保存还原后的设置到文件
SaveSettingsToFile();
// 重新加载设置到UI
LoadSettings();
LogHelper.WriteLogToFile($"成功从备份还原设置: {dlg.FileName}", LogHelper.LogType.Info);
MessageBox.Show("设置已成功还原,部分设置可能需要重启软件后生效。", "还原成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else {
MessageBox.Show("无法解析备份文件,文件可能已损坏", "还原失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
catch (Exception ex) {
LogHelper.WriteLogToFile($"还原设置备份时出错: {ex.Message}", LogHelper.LogType.Error);
MessageBox.Show($"还原备份失败: {ex.Message}", "还原失败", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
@@ -555,6 +555,7 @@ namespace Ink_Canvas {
ToggleSwitchIsEnableResolutionChangeDetection.IsOn = Settings.Advanced.IsEnableResolutionChangeDetection;
ToggleSwitchIsEnableDPIChangeDetection.IsOn = Settings.Advanced.IsEnableDPIChangeDetection;
ToggleSwitchIsEnableAvoidFullScreenHelper.IsOn = Settings.Advanced.IsEnableAvoidFullScreenHelper;
ToggleSwitchIsAutoBackupBeforeUpdate.IsOn = Settings.Advanced.IsAutoBackupBeforeUpdate;
if (Settings.Advanced.IsEnableFullScreenHelper) {
FullScreenHelper.MarkFullscreenWindowTaskbarList(new WindowInteropHelper(this).Handle, true);
}
+3
View File
@@ -418,6 +418,9 @@ namespace Ink_Canvas
[JsonProperty("isEnableAvoidFullScreenHelper")]
public bool IsEnableAvoidFullScreenHelper { get; set; } = false;
[JsonProperty("isAutoBackupBeforeUpdate")]
public bool IsAutoBackupBeforeUpdate { get; set; } = true;
}
public class InkToShape