using SmartCoalApplication.SystemLayer; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SmartCoalApplication { /// /// 最近使用的文件列表 /// internal class MostRecentFiles { private Queue files; private int maxCount; private bool loaded = false; public MostRecentFiles(int maxCount) { this.maxCount = maxCount; this.files = new Queue(); } public bool Loaded { get { return this.loaded; } } public int MaxCount { get { return this.maxCount; } } public MostRecentFile[] GetFileList() { if (!Loaded) { LoadMruList(); } object[] array = files.ToArray(); MostRecentFile[] mrfArray = new MostRecentFile[array.Length]; array.CopyTo(mrfArray, 0); return mrfArray; } public bool Contains(string fileName) { if (!Loaded) { LoadMruList(); } string lcFileName = fileName.ToLower(); foreach (MostRecentFile mrf in files) { string lcMrf = mrf.FileName.ToLower(); if (0 == String.Compare(lcMrf, lcFileName)) { return true; } } return false; } public void Add(MostRecentFile mrf) { if (!Loaded) { LoadMruList(); } if (!Contains(mrf.FileName)) { files.Enqueue(mrf); while (files.Count > maxCount) { files.Dequeue(); } } } public void Remove(string fileName) { if (!Loaded) { LoadMruList(); } if (!Contains(fileName)) { return; } Queue newQueue = new Queue(); foreach (MostRecentFile mrf in files) { if (0 != string.Compare(mrf.FileName, fileName, true)) { newQueue.Enqueue(mrf); } } this.files = newQueue; } public void Clear() { if (!Loaded) { LoadMruList(); } foreach (MostRecentFile mrf in this.GetFileList()) { Remove(mrf.FileName); } } public void LoadMruList() { try { this.loaded = true; Clear(); for (int i = 0; i < MaxCount; ++i) { try { string mruName = "MRU" + i.ToString(); string fileName = (string)Settings.CurrentUser.GetString(mruName); if (fileName != null) { //Image thumb = Settings.CurrentUser.GetImage(mruName + "Thumb"); if (fileName != null)// && thumb != null { MostRecentFile mrf = new MostRecentFile(fileName, null);//thumb Add(mrf); } } } catch { break; } } } catch (Exception) { Clear(); } } public void SaveMruList() { if (Loaded) { MostRecentFile[] mrfArray = GetFileList(); for (int i = 0; i < MaxCount; ++i) { string mruName = "MRU" + i.ToString(); string mruThumbName = mruName + "Thumb"; if (i >= mrfArray.Length) { Settings.CurrentUser.Delete(mruName); Settings.CurrentUser.Delete(mruThumbName); } else { MostRecentFile mrf = mrfArray[i]; Settings.CurrentUser.SetString(mruName, mrf.FileName); } } } } } }