public AddFile ( string fileName ) : void | ||
fileName | string | The path of the recent file. |
return | void |
public void AddFile(string fileName)
{
if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("The argument fileName must not be null or empty.", nameof(fileName)); }
RecentFile recentFile = recentFiles.FirstOrDefault(r => r.Path == fileName);
if (recentFile != null)
{
int oldIndex = recentFiles.IndexOf(recentFile);
int newIndex = recentFile.IsPinned ? 0 : PinCount;
if (oldIndex != newIndex)
{
recentFiles.Move(oldIndex, newIndex);
}
}
else
{
if (PinCount < maxFilesNumber)
{
if (recentFiles.Count >= maxFilesNumber)
{
RemoveAt(recentFiles.Count - 1);
}
Insert(PinCount, new RecentFile(fileName));
}
}
}
public void SetMaxFilesNumber() { RecentFileList recentFileList = new RecentFileList(); recentFileList.AddFile("Doc4"); recentFileList.AddFile("Doc3"); recentFileList.AddFile("Doc2"); recentFileList.AddFile("Doc1"); Assert.IsTrue(recentFileList.RecentFiles.Select(f => f.Path).SequenceEqual(new [] { "Doc1", "Doc2", "Doc3", "Doc4" })); // Set a lower number than items are in the list => expect that the list is truncated. recentFileList.MaxFilesNumber = 3; Assert.AreEqual(3, recentFileList.MaxFilesNumber); Assert.IsTrue(recentFileList.RecentFiles.Select(f => f.Path).SequenceEqual(new[] { "Doc1", "Doc2", "Doc3" })); AssertHelper.ExpectedException<ArgumentException>(() => recentFileList.MaxFilesNumber = -3); }