MostRecentFiles.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using SmartCoalApplication.SystemLayer;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SmartCoalApplication
  9. {
  10. /// <summary>
  11. /// 最近使用的文件列表
  12. /// </summary>
  13. internal class MostRecentFiles
  14. {
  15. private Queue files;
  16. private int maxCount;
  17. private bool loaded = false;
  18. public MostRecentFiles(int maxCount)
  19. {
  20. this.maxCount = maxCount;
  21. this.files = new Queue();
  22. }
  23. public bool Loaded
  24. {
  25. get
  26. {
  27. return this.loaded;
  28. }
  29. }
  30. public int MaxCount
  31. {
  32. get
  33. {
  34. return this.maxCount;
  35. }
  36. }
  37. public MostRecentFile[] GetFileList()
  38. {
  39. if (!Loaded)
  40. {
  41. LoadMruList();
  42. }
  43. object[] array = files.ToArray();
  44. MostRecentFile[] mrfArray = new MostRecentFile[array.Length];
  45. array.CopyTo(mrfArray, 0);
  46. return mrfArray;
  47. }
  48. public bool Contains(string fileName)
  49. {
  50. if (!Loaded)
  51. {
  52. LoadMruList();
  53. }
  54. string lcFileName = fileName.ToLower();
  55. foreach (MostRecentFile mrf in files)
  56. {
  57. string lcMrf = mrf.FileName.ToLower();
  58. if (0 == String.Compare(lcMrf, lcFileName))
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65. public void Add(MostRecentFile mrf)
  66. {
  67. if (!Loaded)
  68. {
  69. LoadMruList();
  70. }
  71. if (!Contains(mrf.FileName))
  72. {
  73. files.Enqueue(mrf);
  74. while (files.Count > maxCount)
  75. {
  76. files.Dequeue();
  77. }
  78. }
  79. }
  80. public void Remove(string fileName)
  81. {
  82. if (!Loaded)
  83. {
  84. LoadMruList();
  85. }
  86. if (!Contains(fileName))
  87. {
  88. return;
  89. }
  90. Queue newQueue = new Queue();
  91. foreach (MostRecentFile mrf in files)
  92. {
  93. if (0 != string.Compare(mrf.FileName, fileName, true))
  94. {
  95. newQueue.Enqueue(mrf);
  96. }
  97. }
  98. this.files = newQueue;
  99. }
  100. public void Clear()
  101. {
  102. if (!Loaded)
  103. {
  104. LoadMruList();
  105. }
  106. foreach (MostRecentFile mrf in this.GetFileList())
  107. {
  108. Remove(mrf.FileName);
  109. }
  110. }
  111. public void LoadMruList()
  112. {
  113. try
  114. {
  115. this.loaded = true;
  116. Clear();
  117. for (int i = 0; i < MaxCount; ++i)
  118. {
  119. try
  120. {
  121. string mruName = "MRU" + i.ToString();
  122. string fileName = (string)Settings.CurrentUser.GetString(mruName);
  123. if (fileName != null)
  124. {
  125. //Image thumb = Settings.CurrentUser.GetImage(mruName + "Thumb");
  126. if (fileName != null)// && thumb != null
  127. {
  128. MostRecentFile mrf = new MostRecentFile(fileName, null);//thumb
  129. Add(mrf);
  130. }
  131. }
  132. }
  133. catch
  134. {
  135. break;
  136. }
  137. }
  138. }
  139. catch (Exception)
  140. {
  141. Clear();
  142. }
  143. }
  144. public void SaveMruList()
  145. {
  146. if (Loaded)
  147. {
  148. MostRecentFile[] mrfArray = GetFileList();
  149. for (int i = 0; i < MaxCount; ++i)
  150. {
  151. string mruName = "MRU" + i.ToString();
  152. string mruThumbName = mruName + "Thumb";
  153. if (i >= mrfArray.Length)
  154. {
  155. Settings.CurrentUser.Delete(mruName);
  156. Settings.CurrentUser.Delete(mruThumbName);
  157. }
  158. else
  159. {
  160. MostRecentFile mrf = mrfArray[i];
  161. Settings.CurrentUser.SetString(mruName, mrf.FileName);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }