SettingHelper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. Settings.Default.Save();
  54. }
  55. public static void RemoveRecentOpenFile(string filePath)
  56. {
  57. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  58. return;
  59. OpenFileInfo deleteItem = null;
  60. foreach(var item in Settings.Default.RecentOpenFiles)
  61. {
  62. if (item.FilePath.ToLower() == filePath.ToLower())
  63. {
  64. deleteItem = item;
  65. break;
  66. }
  67. }
  68. if(deleteItem!=null)
  69. {
  70. DeleteByPath(deleteItem.ThumbImgPath);
  71. Settings.Default.RecentOpenFiles.Remove(deleteItem);
  72. Settings.Default.Save();
  73. }
  74. }
  75. public static void DeleteByPath(string path)
  76. {
  77. try
  78. {
  79. if (!string.IsNullOrEmpty(path) && System.IO.File.Exists(path))
  80. System.IO.File.Delete(path);
  81. }
  82. catch { }
  83. }
  84. public static void RemoveAllRecentOpenFiles()
  85. {
  86. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  87. return;
  88. string folderPath = System.IO.Path.Combine(App.CurrentPath, "CoverImage");
  89. DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
  90. if (directoryInfo.Exists)
  91. {
  92. var files = directoryInfo.GetFiles();
  93. foreach (var file in files)
  94. {
  95. if (file.Exists)
  96. file.Delete();
  97. }
  98. }
  99. Settings.Default.RecentOpenFiles.Clear();
  100. Settings.Default.Save();
  101. }
  102. public static OpenFileInfo GetFileInfo(string filePath)
  103. {
  104. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0)
  105. return null;
  106. foreach(var info in Settings.Default.RecentOpenFiles)
  107. {
  108. if (info.FilePath == filePath)
  109. return info;
  110. }
  111. return null;
  112. }
  113. public static void SetFileInfo(OpenFileInfo openFileInfo)
  114. {
  115. try
  116. {
  117. if (Settings.Default.RecentOpenFiles == null || Settings.Default.RecentOpenFiles.Count == 0 || openFileInfo == null)
  118. return;
  119. for (int i = 0; i < Settings.Default.RecentOpenFiles.Count; i++)
  120. {
  121. if (Settings.Default.RecentOpenFiles[i].FilePath == openFileInfo.FilePath)
  122. {
  123. Settings.Default.RecentOpenFiles[i] = openFileInfo;
  124. Settings.Default.Save();//保存时有可能会文件被占用
  125. return;
  126. }
  127. }
  128. }
  129. catch { }
  130. }
  131. public static DefaultAnnotProperty GetAnnotDefaultProperty(AnnotArgsType annotToolType,string saveKey="")
  132. {
  133. //if (Settings.Default.DefautAnnotProperties != null)
  134. //{
  135. // if(saveKey=="")
  136. // {
  137. // return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType);
  138. // }
  139. // return Settings.Default.DefautAnnotProperties.GetAnnotProperty(annotToolType,saveKey);
  140. //}
  141. return null;
  142. }
  143. public static void SetAnnotDefaultProperty(DefaultAnnotProperty annotProperty)
  144. {
  145. //if (Settings.Default.DefautAnnotProperties == null)
  146. //{
  147. // Settings.Default.DefautAnnotProperties = new DefaultAnnotProperties();
  148. //}
  149. //Settings.Default.DefautAnnotProperties.SetAnnotProperty(annotProperty);
  150. //Settings.Default.Save();
  151. }
  152. private static string KeyAlaphabet = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
  153. public static char NumberToChar(int num)
  154. {
  155. if (num >= 0 && num <= 35)
  156. {
  157. return KeyAlaphabet[num];
  158. }
  159. throw new ArgumentException("Wrong num value.", "ch");
  160. }
  161. /// <summary>
  162. /// AES加密
  163. /// </summary>
  164. /// <param name="text">加密字符</param>
  165. /// <param name="password">加密的密码</param>
  166. /// <returns></returns>
  167. public static string AESEncrypt(string text, string password)
  168. {
  169. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  170. rijndaelCipher.Mode = CipherMode.CBC;
  171. rijndaelCipher.Padding = PaddingMode.PKCS7;
  172. rijndaelCipher.KeySize = 128;
  173. rijndaelCipher.BlockSize = 128;
  174. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  175. byte[] keyBytes = new byte[16];
  176. int len = pwdBytes.Length;
  177. if (len > keyBytes.Length) len = keyBytes.Length;
  178. Array.Copy(pwdBytes, keyBytes, len);
  179. rijndaelCipher.Key = keyBytes;
  180. rijndaelCipher.IV = new byte[16];
  181. ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
  182. byte[] plainText = Encoding.UTF8.GetBytes(text);
  183. byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
  184. return Convert.ToBase64String(cipherBytes);
  185. }
  186. /// <summary>
  187. /// AES解密
  188. /// </summary>
  189. /// <param name="text"></param>
  190. /// <param name="password"></param>
  191. /// <returns></returns>
  192. public static string AESDecrypt(string text, string password)
  193. {
  194. RijndaelManaged rijndaelCipher = new RijndaelManaged();
  195. rijndaelCipher.Mode = CipherMode.CBC;
  196. rijndaelCipher.Padding = PaddingMode.PKCS7;
  197. rijndaelCipher.KeySize = 128;
  198. rijndaelCipher.BlockSize = 128;
  199. byte[] encryptedData = Convert.FromBase64String(text);
  200. byte[] pwdBytes = Encoding.UTF8.GetBytes(password);
  201. byte[] keyBytes = new byte[16];
  202. int len = pwdBytes.Length;
  203. if (len > keyBytes.Length) len = keyBytes.Length;
  204. Array.Copy(pwdBytes, keyBytes, len);
  205. rijndaelCipher.Key = keyBytes;
  206. rijndaelCipher.IV = new byte[16];
  207. ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
  208. byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
  209. return Encoding.UTF8.GetString(plainText);
  210. }
  211. public static ProductActiveInfo GetProductActiveInfo()
  212. {
  213. //if (Settings.Default.ProductActiveStore != null)
  214. //{
  215. // ProductActiveInfo activeInfo = new ProductActiveInfo();
  216. // ProductActiveStore storeInfo= Settings.Default.ProductActiveStore;
  217. // Random random = new Random((int)storeInfo.UpdateTime.Ticks);
  218. // StringBuilder keyBuilder = new StringBuilder();
  219. // for (int i = 0; i < 16; i++)
  220. // {
  221. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  222. // }
  223. // string checkData = AESEncrypt(storeInfo.SignData, keyBuilder.ToString());
  224. // MD5 mD5 = MD5.Create();
  225. // byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  226. // StringBuilder checkBuilder = new StringBuilder();
  227. // foreach (byte code in hashCode)
  228. // {
  229. // checkBuilder.Append(code.ToString("X2"));
  230. // }
  231. // if (checkBuilder.ToString() == storeInfo.CheckSum)
  232. // {
  233. // string decryptData = AESDecrypt(storeInfo.SignData, keyBuilder.ToString());
  234. // activeInfo = JsonConvert.DeserializeObject<ProductActiveInfo>(decryptData);
  235. // }
  236. // return activeInfo;
  237. //}
  238. return null;
  239. }
  240. public static void SetProductActiveStore(ProductActiveInfo activeInfo)
  241. {
  242. //ProductActiveStore activeStore = new ProductActiveStore();
  243. //activeStore.UpdateTime = DateTime.Now;
  244. //string rawData = JsonConvert.SerializeObject(activeInfo);
  245. //Random random = new Random((int)activeStore.UpdateTime.Ticks);
  246. //StringBuilder keyBuilder = new StringBuilder();
  247. //for (int i = 0; i < 16; i++)
  248. //{
  249. // keyBuilder.Append(NumberToChar(random.Next(0, 35)));
  250. //}
  251. //activeStore.SignData = AESEncrypt(rawData, keyBuilder.ToString());
  252. //string checkData = AESEncrypt(activeStore.SignData, keyBuilder.ToString());
  253. //MD5 mD5 = MD5.Create();
  254. //byte[] hashCode = mD5.ComputeHash(Encoding.UTF8.GetBytes(checkData));
  255. //StringBuilder checkBuilder = new StringBuilder();
  256. //foreach(byte code in hashCode)
  257. //{
  258. // checkBuilder.Append(code.ToString("X2"));
  259. //}
  260. //activeStore.CheckSum = checkBuilder.ToString();
  261. //Settings.Default.ProductActiveStore = activeStore;
  262. //Settings.Default.Save();
  263. }
  264. }
  265. }