From 1f147f1714567b550f1950d84802a1aa983e6658 Mon Sep 17 00:00:00 2001
From: lanboss <547734658@qq.com>
Date: Thu, 8 May 2025 13:01:57 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=87=E4=BB=BDjson?=
=?UTF-8?q?=E6=A0=BC=E5=BC=8F=EF=BC=9B=E8=A7=A3=E5=86=B3=E6=97=A0=E5=A4=87?=
=?UTF-8?q?=E4=BB=BD=E7=9B=AE=E5=BD=95=E6=8A=A5=E9=94=99=E7=9A=84bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Control/UserControls/Config/OtherControl.xaml | 2 +
.../UserControls/Config/OtherControl.xaml.cs | 5 +
Util/CommonCode.cs | 188 +++++++++++++-----
ViewModel/AppConfig.cs | 2 +-
ViewModel/IconInfo.cs | 5 +-
5 files changed, 145 insertions(+), 57 deletions(-)
diff --git a/Control/UserControls/Config/OtherControl.xaml b/Control/UserControls/Config/OtherControl.xaml
index 91b630c..3918c8f 100644
--- a/Control/UserControls/Config/OtherControl.xaml
+++ b/Control/UserControls/Config/OtherControl.xaml
@@ -21,6 +21,8 @@
+
+
f.Extension.Equals(".bak", StringComparison.OrdinalIgnoreCase)).ToArray(); ;
- if (files.Length > 0)
+ if (!dirInfo.Exists)
{
- FileInfo[] sortedFiles = files.OrderByDescending(file => file.CreationTime).ToArray();
- //循环获取可用备份文件
- string bakFilePath = "";
- foreach (FileInfo bakFile in sortedFiles)
+ MessageBox.Show($"{dirInfo.FullName}指定目录查找备份,但该目录不存在,已自动创建,如果有备份,请将备份放在该目录下");
+ Directory.CreateDirectory(dirInfo.FullName);
+ return new AppData();
+ }
+ int fileCount = dirInfo.GetFiles()
+ .Count(f => f.Extension.Equals(".bak", StringComparison.OrdinalIgnoreCase));
+ if (fileCount > 0)
+ {
+ FileInfo[] files = dirInfo.GetFiles()
+ .Where(f => f.Extension.Equals(".bak", StringComparison.OrdinalIgnoreCase)).ToArray(); ;
+ if (files.Length > 0)
{
- if (!Directory.Exists(Constants.DATA_FILE_TEMP_DIR_PATH)) { Directory.CreateDirectory(Constants.DATA_FILE_TEMP_DIR_PATH); }
- bakFilePath = Constants.DATA_FILE_TEMP_DIR_PATH + "\\" + bakFile.Name;
- try
+ FileInfo[] sortedFiles = files.OrderByDescending(file => file.CreationTime).ToArray();
+ //循环获取可用备份文件
+ string bakFilePath = "";
+ foreach (FileInfo bakFile in sortedFiles)
{
- File.Copy(bakFile.FullName, bakFilePath, true);
- using (FileStream fs = new FileStream(bakFilePath, FileMode.Open))
+ if (!Directory.Exists(Constants.DATA_FILE_TEMP_DIR_PATH)) { Directory.CreateDirectory(Constants.DATA_FILE_TEMP_DIR_PATH); }
+ bakFilePath = Constants.DATA_FILE_TEMP_DIR_PATH + "\\" + bakFile.Name;
+ try
{
- BinaryFormatter bf = new BinaryFormatter();
- appData = bf.Deserialize(fs) as AppData;
+ File.Copy(bakFile.FullName, bakFilePath, true);
+ using (FileStream fs = new FileStream(bakFilePath, FileMode.Open))
+ {
+ BinaryFormatter bf = new BinaryFormatter();
+ appData = bf.Deserialize(fs) as AppData;
+ }
+ DialogMsg msg = new DialogMsg();
+ msg.msg = "不幸的是, GeekDesk当前的数据文件已经损坏, " +
+ "现在已经启用系统自动备份的数据\n\n" +
+ "如果你有较新的备份, " +
+ "请退出GeekDesk, " +
+ "将备份文件重命名为:Data, " +
+ "然后将Data覆盖到GeekDesk的根目录即可\n\n" +
+ "启用的备份文件为: \n" + bakFilePath +
+ "\n\n如果当前数据就是你想要的数据, 那么请不用管它";
+ GlobalMsgNotification gm = new GlobalMsgNotification(msg);
+ HandyControl.Controls.Notification ntf = HandyControl.Controls.Notification.Show(gm, ShowAnimation.Fade, true);
+ gm.ntf = ntf;
+ File.Delete(bakFilePath);
+ SaveAppData(appData, Constants.DATA_FILE_PATH);
+ return appData;
}
- DialogMsg msg = new DialogMsg();
- msg.msg = "不幸的是, GeekDesk当前的数据文件已经损坏, " +
- "现在已经启用系统自动备份的数据\n\n" +
- "如果你有较新的备份, " +
- "请退出GeekDesk, " +
- "将备份文件重命名为:Data, " +
- "然后将Data覆盖到GeekDesk的根目录即可\n\n" +
- "启用的备份文件为: \n" + bakFilePath +
- "\n\n如果当前数据就是你想要的数据, 那么请不用管它";
- GlobalMsgNotification gm = new GlobalMsgNotification(msg);
- HandyControl.Controls.Notification ntf = HandyControl.Controls.Notification.Show(gm, ShowAnimation.Fade, true);
- gm.ntf = ntf;
- File.Delete(bakFilePath);
- SaveAppData(appData, Constants.DATA_FILE_PATH);
- return appData;
- }
- catch {
- if (File.Exists(bakFilePath))
+ catch
{
- File.Delete(bakFilePath);
+ if (File.Exists(bakFilePath))
+ {
+ File.Delete(bakFilePath);
+ }
}
}
+ MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
+ Application.Current.Shutdown();
+ return new AppData();
}
- MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
- Application.Current.Shutdown();
- return new AppData();
- } else
+ else
+ {
+ var temp = LoadJson(dirInfo);
+ if (temp != null)
+ return temp;
+ MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
+
+
+ Application.Current.Shutdown();
+ return new AppData();
+ }
+ }
+ else
{
- MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
- Application.Current.Shutdown();
+ var temp = LoadJson(dirInfo);
+ if (temp != null)
+ return temp;
+ MessageBox.Show($"{dirInfo}指定目录不存在备份不");
return new AppData();
}
-
// if (File.Exists(Constants.DATA_FILE_BAK_PATH))
//{
// try
@@ -150,7 +176,32 @@ namespace GeekDesk.Util
}
}
}
+ static AppData LoadJson(DirectoryInfo dirInfo)
+ {
+ FileInfo[] filesJson = dirInfo.GetFiles()
+ .Where(f => f.Extension.Equals(".json", StringComparison.OrdinalIgnoreCase)).ToArray(); ;
+ if (filesJson.Length > 0)
+ {
+ FileInfo[] sortedFiles = filesJson.OrderByDescending(file => file.CreationTime).ToArray();
+ var item = sortedFiles.FirstOrDefault();
+ {
+ try
+ {
+
+ var json = File.ReadAllText(item.FullName);
+ var jsonObj = JsonConvert.DeserializeObject(json) ?? new AppData();
+ return jsonObj;
+
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show($"解析失败,{ex.Message}");
+ }
+ }
+ }
+ return null;
+ }
private readonly static object _MyLock = new object();
///
/// 保存app 数据
@@ -160,6 +211,8 @@ namespace GeekDesk.Util
{
lock (_MyLock)
{
+ if (appData == null)
+ return;
//if (filePath.Equals(Constants.DATA_FILE_BAK_PATH))
//{
// appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
@@ -198,7 +251,8 @@ namespace GeekDesk.Util
return uuid;
}
}
- } catch (Exception) { }
+ }
+ catch (Exception) { }
return "ERROR_UUID_GeneraterUUID_" + Constants.MY_UUID;
}
@@ -212,32 +266,58 @@ namespace GeekDesk.Util
{
return reader.ReadToEnd().Trim();
}
- } else
+ }
+ else
{
return GeneraterUUID();
}
- } catch(Exception) { }
+ }
+ catch (Exception) { }
return "ERROR_UUID_GetUniqueUUID_" + Constants.MY_UUID;
}
public static void BakAppData()
{
-
- SaveFileDialog sfd = new SaveFileDialog
+ MessageBoxResult r = MessageBox.Show("保存为二进制或json(yes:二进制;no:json,json仅供查看,默认不能用于加载,在原二进制加载出错时尝试加载json,但部分图不保存)?", "提示", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
+ if (r == MessageBoxResult.Yes)
{
- Title = "备份文件",
- Filter = "bak文件(*.bak)|*.bak",
- FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
- };
- if (sfd.ShowDialog() == true)
+ SaveFileDialog sfd = new SaveFileDialog
+ {
+ Title = "备份文件",
+ Filter = "bak文件(*.bak)|*.bak",
+ FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
+ };
+ if (sfd.ShowDialog() == true)
+ {
+ using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
+ {
+ BinaryFormatter bf = new BinaryFormatter();
+ bf.Serialize(fs, MainWindow.appData);
+ }
+ }
+ }
+ if (r == MessageBoxResult.No)
{
- using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
+ SaveFileDialog sfd = new SaveFileDialog
{
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(fs, MainWindow.appData);
+ Title = "备份文件",
+ Filter = "json文件(*.json)|*.json",
+ FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".json",
+ };
+ if (sfd.ShowDialog() == true)
+ {
+ var json = JsonConvert.SerializeObject(MainWindow.appData, Formatting.Indented);
+ File.WriteAllText(sfd.FileName, json);
+
}
}
+ if (r == MessageBoxResult.Cancel)
+ {
+
+ }
+
+
}
@@ -382,7 +462,7 @@ namespace GeekDesk.Util
}
}
catch (Exception) { }
-
+
}
diff --git a/ViewModel/AppConfig.cs b/ViewModel/AppConfig.cs
index 99d3a49..4a5cce3 100644
--- a/ViewModel/AppConfig.cs
+++ b/ViewModel/AppConfig.cs
@@ -916,7 +916,7 @@ namespace GeekDesk.ViewModel
}
}
-
+ [JsonIgnore] // 忽略此属性,避免直接序列化 BitmapImage
public BitmapImage BitmapImage
{
get
diff --git a/ViewModel/IconInfo.cs b/ViewModel/IconInfo.cs
index fa35e4a..89f2bb4 100644
--- a/ViewModel/IconInfo.cs
+++ b/ViewModel/IconInfo.cs
@@ -1,5 +1,6 @@
using GeekDesk.Constant;
using GeekDesk.Util;
+using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Windows.Media.Imaging;
@@ -272,7 +273,7 @@ namespace GeekDesk.ViewModel
}
}
-
+ [JsonIgnore] // 忽略此属性,避免直接序列化 BitmapImage
public BitmapImage BitmapImage
{
get
@@ -287,7 +288,7 @@ namespace GeekDesk.ViewModel
OnPropertyChanged("BitmapImage");
}
}
-
+ [JsonIgnore] // 忽略此属性,避免直接序列化 BitmapImage
public BitmapImage BitmapImage_NoWrite
{
get
--
Gitee