SettingHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using ComPDFKitViewer.AnnotEvent;
  2. using PDFSettings;
  3. using PDFSettings.Settings;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Dynamic;
  9. using System.Windows;
  10. using System.IO;
  11. using PDF_Office.Properties;
  12. using PDF_Office;
  13. namespace PDF_Office.Helper
  14. {
  15. public class SettingHelper
  16. {
  17. public static void SortRecentOpenFiles(string filePath)
  18. {
  19. if (Settings.Default.RecentOpenFiles == null)
  20. Settings.Default.RecentOpenFiles = new RecentOpenFiles();
  21. OpenFileInfo existFile = null;
  22. foreach (var file in Settings.Default.RecentOpenFiles)
  23. {
  24. if (file.FilePath.ToLower() == filePath.ToLower())
  25. {
  26. if(file.FilePath != filePath)
  27. {
  28. file.FilePath = filePath;
  29. file.FileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
  30. }
  31. file.LastOpenTime = DateTime.Now;
  32. existFile = file;
  33. break;
  34. }
  35. }
  36. if (existFile != null)
  37. {
  38. if (Settings.Default.RecentOpenFiles.IndexOf(existFile) != 0)
  39. {
  40. Settings.Default.RecentOpenFiles.Remove(existFile);
  41. Settings.Default.RecentOpenFiles.Insert(0, existFile);
  42. }
  43. }
  44. else
  45. {
  46. OpenFileInfo fileInfo = new OpenFileInfo();
  47. fileInfo.FileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
  48. fileInfo.FilePath = filePath;
  49. fileInfo.ThumbImgPath = "";
  50. fileInfo.LastOpenTime = DateTime.Now;
  51. PDF_Office.Properties.Settings.Default.RecentOpenFiles.Insert(0, fileInfo);
  52. }
  53. //限制最近文件列表显示的最大条数
  54. if(Settings.Default.RecentOpenFiles.Count>Settings.Default.AppProperties.Description.FileCountInRecentFiles)
  55. {
  56. Settings.Default.RecentOpenFiles.RemoveRange(Settings.Default.AppProperties.Description.FileCountInRecentFiles, Settings.Default.RecentOpenFiles.Count-Settings.Default.AppProperties.Description.FileCountInRecentFiles);
  57. }
  58. Settings.Default.Save();
  59. }
  60. public static void RemoveRecentOpenFile(string filePath)
  61. {
  62. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  63. return;
  64. OpenFileInfo deleteItem = null;
  65. foreach(var item in Settings.Default.RecentOpenFiles)
  66. {
  67. if (item.FilePath.ToLower() == filePath.ToLower())
  68. {
  69. deleteItem = item;
  70. break;
  71. }
  72. }
  73. if(deleteItem!=null)
  74. {
  75. DeleteByPath(deleteItem.ThumbImgPath);
  76. Settings.Default.RecentOpenFiles.Remove(deleteItem);
  77. Settings.Default.Save();
  78. }
  79. }
  80. public static void DeleteByPath(string path)
  81. {
  82. try
  83. {
  84. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  85. System.IO.File.Delete(path);
  86. }
  87. catch { }
  88. }
  89. public static void RemoveAllRecentOpenFiles()
  90. {
  91. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  92. return;
  93. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  94. DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
  95. if (directoryInfo.Exists)
  96. {
  97. var files = directoryInfo.GetFiles();
  98. foreach (var file in files)
  99. {
  100. if (file.Exists)
  101. file.Delete();
  102. }
  103. }
  104. Settings.Default.RecentOpenFiles.Clear();
  105. Settings.Default.Save();
  106. }
  107. public static OpenFileInfo GetFileInfo(string filePath)
  108. {
  109. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  110. return null;
  111. foreach(var info in Settings.Default.RecentOpenFiles)
  112. {
  113. if (info.FilePath == filePath)
  114. return info;
  115. }
  116. return null;
  117. }
  118. public static void SetFileInfo(OpenFileInfo openFileInfo)
  119. {
  120. try
  121. {
  122. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0 || openFileInfo == null)
  123. return;
  124. for (int i = 0; i < Settings.Default.RecentOpenFiles.Count; i++)
  125. {
  126. if (Settings.Default.RecentOpenFiles[i].FilePath == openFileInfo.FilePath)
  127. {
  128. Settings.Default.RecentOpenFiles[i] = openFileInfo;
  129. Settings.Default.Save();//保存时有可能会文件被占用
  130. return;
  131. }
  132. }
  133. }
  134. catch { }
  135. }
  136. public static DefaultAnnotProperty GetAnnotDefaultProperty(AnnotArgsType annotToolType,string saveKey="")
  137. {
  138. if (Settings.Default.DefautAnnotProperties != null)
  139. {
  140. if (saveKey == "")
  141. {
  142. return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType);
  143. }
  144. return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType, saveKey);
  145. }
  146. return null;
  147. }
  148. public static void SetAnnotDefaultProperty(DefaultAnnotProperty annotProperty)
  149. {
  150. if (Settings.Default.DefautAnnotProperties == null)
  151. {
  152. Settings.Default.DefautAnnotProperties = new DefaultAnnotProperties();
  153. }
  154. Settings.Default.DefautAnnotProperties.SetAnnotProperty(annotProperty);
  155. Settings.Default.Save();
  156. }
  157. private static string KeyAlaphabet = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
  158. public static char NumberToChar(int num)
  159. {
  160. if (num >= 0 && num <= 35)
  161. {
  162. return KeyAlaphabet[num];
  163. }
  164. throw new ArgumentException("Wrong num value.", "ch");
  165. }
  166. /// <summary>
  167. /// AES加密
  168. /// </summary>
  169. /// <param name="text">加密字符</param>
  170. /// <param name="password">加密的密码</param>
  171. /// <returns></returns>
  172. public static string AESEncrypt(string text, string password)
  173. {
  174. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  175. rijndaelCipher.Mode = CipherMode.CBC;
  176. rijndaelCipher.Padding = PaddingMode.PKCS7;
  177. rijndaelCipher.KeySize = 128;
  178. rijndaelCipher.BlockSize = 128;
  179. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  180. byte[] keyBytes = new byte[16];
  181. int len = pwdBytes.Length;
  182. if (len > keyBytes.Length) len = keyBytes.Length;
  183. Array.Copy(pwdBytes, keyBytes, len);
  184. rijndaelCipher.Key = keyBytes;
  185. rijndaelCipher.IV = new byte[16];
  186. ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
  187. byte[] plainText = Encoding.UTF8.GetBytes(text);
  188. byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
  189. return Convert.ToBase64String(cipherBytes);
  190. }
  191. /// <summary>
  192. /// AES解密
  193. /// </summary>
  194. /// <param name="text"></param>
  195. /// <param name="password"></param>
  196. /// <returns></returns>
  197. public static string AESDecrypt(string text, string password)
  198. {
  199. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  200. rijndaelCipher.Mode = CipherMode.CBC;
  201. rijndaelCipher.Padding = PaddingMode.PKCS7;
  202. rijndaelCipher.KeySize = 128;
  203. rijndaelCipher.BlockSize = 128;
  204. byte[] encryptedData = Convert.FromBase64String(text);
  205. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  206. byte[] keyBytes = new byte[16];
  207. int len = pwdBytes.Length;
  208. if (len > keyBytes.Length) len = keyBytes.Length;
  209. Array.Copy(pwdBytes, keyBytes, len);
  210. rijndaelCipher.Key = keyBytes;
  211. rijndaelCipher.IV = new byte[16];
  212. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  213. byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  214. return Encoding.UTF8.GetString(plainText);
  215. }
  216. public static ProductActiveInfo GetProductActiveInfo()
  217. {
  218. //if (Settings.Default.ProductActiveStore != null)
  219. //{
  220. // ProductActiveInfo activeInfo = new ProductActiveInfo();
  221. // ProductActiveStore storeInfo= Settings.Default.ProductActiveStore;
  222. // Random random = new Random((int)storeInfo.UpdateTime.Ticks);
  223. // StringBuilder keyBuilder = new StringBuilder();
  224. // for (int i = 0; i < 16; i++)
  225. // {
  226. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  227. // }
  228. // string checkData = AESEncrypt(storeInfo.SignData, keyBuilder.ToString());
  229. // MD5 mD5 = MD5.Create();
  230. // byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  231. // StringBuilder checkBuilder = new StringBuilder();
  232. // foreach (byte code in hashCode)
  233. // {
  234. // checkBuilder.Append(code.ToString("X2"));
  235. // }
  236. // if (checkBuilder.ToString() == storeInfo.CheckSum)
  237. // {
  238. // string decryptData = AESDecrypt(storeInfo.SignData, keyBuilder.ToString());
  239. // activeInfo = JsonConvert.DeserializeObject<ProductActiveInfo>(decryptData);
  240. // }
  241. // return activeInfo;
  242. //}
  243. return null;
  244. }
  245. public static void SetProductActiveStore(ProductActiveInfo activeInfo)
  246. {
  247. //ProductActiveStore activeStore = new ProductActiveStore();
  248. //activeStore.UpdateTime = DateTime.Now;
  249. //string rawData = JsonConvert.SerializeObject(activeInfo);
  250. //Random random = new Random((int)activeStore.UpdateTime.Ticks);
  251. //StringBuilder keyBuilder = new StringBuilder();
  252. //for (int i = 0; i < 16; i++)
  253. //{
  254. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  255. //}
  256. //activeStore.SignData = AESEncrypt(rawData, keyBuilder.ToString());
  257. //string checkData = AESEncrypt(activeStore.SignData, keyBuilder.ToString());
  258. //MD5 mD5 = MD5.Create();
  259. //byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  260. //StringBuilder checkBuilder = new StringBuilder();
  261. //foreach(byte code in hashCode)
  262. //{
  263. // checkBuilder.Append(code.ToString("X2"));
  264. //}
  265. //activeStore.CheckSum = checkBuilder.ToString();
  266. //Settings.Default.ProductActiveStore = activeStore;
  267. //Settings.Default.Save();
  268. }
  269. }
  270. }