CommonCode.cs 16 KB

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