CommonCode.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Other;
  3. using GeekDesk.ViewModel;
  4. using HandyControl.Data;
  5. using Microsoft.Win32;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Runtime.Serialization.Formatters.Binary;
  14. using System.Windows;
  15. using System.Windows.Media.Imaging;
  16. using static GeekDesk.Control.Other.GlobalMsgNotification;
  17. /// <summary>
  18. /// 提取一些代码
  19. /// </summary>
  20. namespace GeekDesk.Util
  21. {
  22. class CommonCode
  23. {
  24. /// <summary>
  25. /// 获取app 数据
  26. /// </summary>
  27. /// <returns></returns>
  28. public static AppData GetAppDataByFile()
  29. {
  30. AppData appData;
  31. if (!File.Exists(Constants.DATA_FILE_PATH))
  32. {
  33. using (FileStream fs = File.Create(Constants.DATA_FILE_PATH)) { }
  34. appData = new AppData();
  35. SaveAppData(appData, Constants.DATA_FILE_PATH);
  36. return appData;
  37. }
  38. else
  39. {
  40. try
  41. {
  42. using (FileStream fs = new FileStream(Constants.DATA_FILE_PATH, FileMode.Open))
  43. {
  44. BinaryFormatter bf = new BinaryFormatter();
  45. appData = bf.Deserialize(fs) as AppData;
  46. //将菜单密码写入文件
  47. if (!string.IsNullOrEmpty(appData.AppConfig.MenuPassword))
  48. {
  49. SavePassword(appData.AppConfig.MenuPassword);
  50. }
  51. return appData;
  52. }
  53. }
  54. catch
  55. {
  56. DirectoryInfo dirInfo = new DirectoryInfo(Constants.DATA_FILE_BAK_DIR_PATH);
  57. FileInfo[] files = dirInfo.GetFiles()
  58. .Where(f => f.Extension.Equals(".bak", StringComparison.OrdinalIgnoreCase)).ToArray(); ;
  59. if (files.Length > 0)
  60. {
  61. FileInfo[] sortedFiles = files.OrderByDescending(file => file.CreationTime).ToArray();
  62. //循环获取可用备份文件
  63. string bakFilePath = "";
  64. foreach (FileInfo bakFile in sortedFiles)
  65. {
  66. if (!Directory.Exists(Constants.DATA_FILE_TEMP_DIR_PATH)) { Directory.CreateDirectory(Constants.DATA_FILE_TEMP_DIR_PATH); }
  67. bakFilePath = Constants.DATA_FILE_TEMP_DIR_PATH + "\\" + bakFile.Name;
  68. try
  69. {
  70. File.Copy(bakFile.FullName, bakFilePath, true);
  71. using (FileStream fs = new FileStream(bakFilePath, FileMode.Open))
  72. {
  73. BinaryFormatter bf = new BinaryFormatter();
  74. appData = bf.Deserialize(fs) as AppData;
  75. }
  76. DialogMsg msg = new DialogMsg();
  77. msg.msg = "不幸的是, GeekDesk当前的数据文件已经损坏, " +
  78. "现在已经启用系统自动备份的数据\n\n" +
  79. "如果你有较新的备份, " +
  80. "请退出GeekDesk, " +
  81. "将备份文件重命名为:Data, " +
  82. "然后将Data覆盖到GeekDesk的根目录即可\n\n" +
  83. "启用的备份文件为: \n" + bakFilePath +
  84. "\n\n如果当前数据就是你想要的数据, 那么请不用管它";
  85. GlobalMsgNotification gm = new GlobalMsgNotification(msg);
  86. HandyControl.Controls.Notification ntf = HandyControl.Controls.Notification.Show(gm, ShowAnimation.Fade, true);
  87. gm.ntf = ntf;
  88. File.Delete(bakFilePath);
  89. SaveAppData(appData, Constants.DATA_FILE_PATH);
  90. return appData;
  91. }
  92. catch {
  93. if (File.Exists(bakFilePath))
  94. {
  95. File.Delete(bakFilePath);
  96. }
  97. }
  98. }
  99. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  100. Application.Current.Shutdown();
  101. return new AppData();
  102. } else
  103. {
  104. MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  105. Application.Current.Shutdown();
  106. return new AppData();
  107. }
  108. // if (File.Exists(Constants.DATA_FILE_BAK_PATH))
  109. //{
  110. // try
  111. // {
  112. // using (FileStream fs = new FileStream(Constants.DATA_FILE_BAK_PATH, FileMode.Open))
  113. // {
  114. // BinaryFormatter bf = new BinaryFormatter();
  115. // appData = bf.Deserialize(fs) as AppData;
  116. // }
  117. // DialogMsg msg = new DialogMsg();
  118. // msg.msg = "不幸的是, GeekDesk当前的数据文件已经损坏, " +
  119. // "现在已经启用系统自动备份的数据\n\n" +
  120. // "如果你有较新的备份, " +
  121. // "请退出GeekDesk, " +
  122. // "将备份文件重命名为:Data, " +
  123. // "然后将Data覆盖到GeekDesk的根目录即可\n\n" +
  124. // "系统上次备份时间: \n" + appData.AppConfig.SysBakTime +
  125. // "\n\n如果当前数据就是你想要的数据, 那么请不用管它";
  126. // GlobalMsgNotification gm = new GlobalMsgNotification(msg);
  127. // HandyControl.Controls.Notification ntf = HandyControl.Controls.Notification.Show(gm, ShowAnimation.Fade, true);
  128. // gm.ntf = ntf;
  129. // }
  130. // catch
  131. // {
  132. // MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  133. // Application.Current.Shutdown();
  134. // return null;
  135. // }
  136. //}
  137. //else
  138. //{
  139. // MessageBox.Show("不幸的是, GeekDesk当前的数据文件已经损坏\n如果你有备份, 请将备份文件重命名为:Data 然后将Data覆盖到GeekDesk的根目录即可!");
  140. // Application.Current.Shutdown();
  141. // return null;
  142. //}
  143. }
  144. }
  145. }
  146. private readonly static object _MyLock = new object();
  147. /// <summary>
  148. /// 保存app 数据
  149. /// </summary>
  150. /// <param name="appData"></param>
  151. public static void SaveAppData(AppData appData, string filePath)
  152. {
  153. lock (_MyLock)
  154. {
  155. //if (filePath.Equals(Constants.DATA_FILE_BAK_PATH))
  156. //{
  157. // appData.AppConfig.SysBakTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  158. //}
  159. if (!Directory.Exists(filePath.Substring(0, filePath.LastIndexOf("\\"))))
  160. {
  161. Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\")));
  162. }
  163. using (FileStream fs = new FileStream(filePath, FileMode.Create))
  164. {
  165. BinaryFormatter bf = new BinaryFormatter();
  166. bf.Serialize(fs, appData);
  167. }
  168. }
  169. }
  170. public static void SavePassword(string password)
  171. {
  172. using (StreamWriter sw = new StreamWriter(Constants.PW_FILE_BAK_PATH))
  173. {
  174. sw.Write(password);
  175. }
  176. }
  177. private static string GeneraterUUID()
  178. {
  179. try
  180. {
  181. if (!File.Exists(Constants.UUID_FILE_BAK_PATH) || string.IsNullOrEmpty(GetUniqueUUID()))
  182. {
  183. using (StreamWriter sw = new StreamWriter(Constants.UUID_FILE_BAK_PATH))
  184. {
  185. string uuid = Guid.NewGuid().ToString() + "-" + Constants.MY_UUID;
  186. sw.Write(uuid);
  187. return uuid;
  188. }
  189. }
  190. } catch (Exception) { }
  191. return "ERROR_UUID_GeneraterUUID_" + Constants.MY_UUID;
  192. }
  193. public static string GetUniqueUUID()
  194. {
  195. try
  196. {
  197. if (File.Exists(Constants.UUID_FILE_BAK_PATH))
  198. {
  199. using (StreamReader reader = new StreamReader(Constants.UUID_FILE_BAK_PATH))
  200. {
  201. return reader.ReadToEnd().Trim();
  202. }
  203. } else
  204. {
  205. return GeneraterUUID();
  206. }
  207. } catch(Exception) { }
  208. return "ERROR_UUID_GetUniqueUUID_" + Constants.MY_UUID;
  209. }
  210. public static void BakAppData()
  211. {
  212. SaveFileDialog sfd = new SaveFileDialog
  213. {
  214. Title = "备份文件",
  215. Filter = "bak文件(*.bak)|*.bak",
  216. FileName = "Data-GD-" + DateTime.Now.ToString("yyMMdd") + ".bak",
  217. };
  218. if (sfd.ShowDialog() == true)
  219. {
  220. using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create))
  221. {
  222. BinaryFormatter bf = new BinaryFormatter();
  223. bf.Serialize(fs, MainWindow.appData);
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// 根据路径获取文件图标等信息
  229. /// </summary>
  230. /// <param name="path"></param>
  231. /// <returns></returns>
  232. public static IconInfo GetIconInfoByPath(string path)
  233. {
  234. string tempPath = path;
  235. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  236. //string ext = "";
  237. //if (!ImageUtil.IsSystemItem(path))
  238. //{
  239. // ext = System.IO.Path.GetExtension(path).ToLower();
  240. //}
  241. string iconPath;
  242. //if (".lnk".Equals(ext))
  243. //{
  244. string targetPath = FileUtil.GetTargetPathByLnk(path);
  245. iconPath = FileUtil.GetIconPathByLnk(path);
  246. if (targetPath != null)
  247. {
  248. path = targetPath;
  249. }
  250. //}
  251. if (StringUtil.IsEmpty(iconPath))
  252. {
  253. iconPath = path;
  254. }
  255. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  256. IconInfo iconInfo = new IconInfo
  257. {
  258. Path_NoWrite = path,
  259. LnkPath_NoWrite = tempPath,
  260. BitmapImage_NoWrite = bi,
  261. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  262. };
  263. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  264. iconInfo.Name_NoWrite = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  265. if (StringUtil.IsEmpty(iconInfo.Name))
  266. {
  267. iconInfo.Name_NoWrite = path;
  268. }
  269. string relativePath = FileUtil.MakeRelativePath(Constants.APP_DIR + "GeekDesk.exe", iconInfo.Path);
  270. if (!string.IsNullOrEmpty(relativePath) && !relativePath.Equals(iconInfo.Path))
  271. {
  272. iconInfo.RelativePath_NoWrite = relativePath;
  273. }
  274. return iconInfo;
  275. }
  276. public static IconInfo GetIconInfoByPath_NoWrite(string path)
  277. {
  278. string tempPath = path;
  279. //string base64 = ImageUtil.FileImageToBase64(path, System.Drawing.Imaging.ImageFormat.Png);
  280. string ext = "";
  281. if (!ImageUtil.IsSystemItem(path))
  282. {
  283. ext = System.IO.Path.GetExtension(path).ToLower();
  284. }
  285. string iconPath = null;
  286. if (".lnk".Equals(ext))
  287. {
  288. string targetPath = FileUtil.GetTargetPathByLnk(path);
  289. iconPath = FileUtil.GetIconPathByLnk(path);
  290. if (targetPath != null)
  291. {
  292. path = targetPath;
  293. }
  294. }
  295. if (StringUtil.IsEmpty(iconPath))
  296. {
  297. iconPath = path;
  298. }
  299. BitmapImage bi = ImageUtil.GetBitmapIconByPath(iconPath);
  300. IconInfo iconInfo = new IconInfo
  301. {
  302. Path_NoWrite = path,
  303. LnkPath_NoWrite = tempPath,
  304. BitmapImage_NoWrite = bi,
  305. StartArg_NoWrite = FileUtil.GetArgByLnk(tempPath)
  306. };
  307. iconInfo.DefaultImage_NoWrite = iconInfo.ImageByteArr;
  308. iconInfo.Name = System.IO.Path.GetFileNameWithoutExtension(tempPath);
  309. if (StringUtil.IsEmpty(iconInfo.Name))
  310. {
  311. iconInfo.Name_NoWrite = path;
  312. }
  313. return iconInfo;
  314. }
  315. /// <summary>
  316. /// 排序图标
  317. /// </summary>
  318. public static void SortIconList(bool sort = true)
  319. {
  320. try
  321. {
  322. if (MainWindow.appData.AppConfig.IconSortType != SortType.CUSTOM && sort)
  323. {
  324. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  325. //List<IconInfo> list = new List<IconInfo>(menuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList);
  326. List<IconInfo> list;
  327. foreach (MenuInfo menuInfo in menuList)
  328. {
  329. list = new List<IconInfo>(menuInfo.IconList);
  330. switch (MainWindow.appData.AppConfig.IconSortType)
  331. {
  332. case SortType.COUNT_UP:
  333. list.Sort((x, y) => x.Count.CompareTo(y.Count));
  334. break;
  335. case SortType.COUNT_LOW:
  336. list.Sort((x, y) => y.Count.CompareTo(x.Count));
  337. break;
  338. case SortType.NAME_UP:
  339. list.Sort((x, y) => x.Name.CompareTo(y.Name));
  340. break;
  341. case SortType.NAME_LOW:
  342. list.Sort((x, y) => y.Name.CompareTo(x.Name));
  343. break;
  344. }
  345. menuInfo.IconList = new ObservableCollection<IconInfo>(list);
  346. }
  347. MainWindow.appData.AppConfig.SelectedMenuIcons = MainWindow.appData.MenuList[MainWindow.appData.AppConfig.SelectedMenuIndex].IconList;
  348. }
  349. }
  350. catch (Exception) { }
  351. }
  352. /// <summary>
  353. /// 判断鼠标是否在窗口内
  354. /// </summary>
  355. /// <param name="window"></param>
  356. /// <returns></returns>
  357. public static bool MouseInWindow(Window window)
  358. {
  359. double windowHeight = window.Height;
  360. double windowWidth = window.Width;
  361. double windowTop = window.Top;
  362. double windowLeft = window.Left;
  363. //获取鼠标位置
  364. System.Windows.Point p = MouseUtil.GetMousePosition();
  365. double mouseX = p.X;
  366. double mouseY = p.Y;
  367. //鼠标不在窗口上
  368. if (mouseX < windowLeft || mouseX > windowLeft + windowWidth
  369. || mouseY < windowTop || mouseY > windowTop + windowHeight)
  370. {
  371. return false;
  372. }
  373. return true;
  374. }
  375. }
  376. }