System.Waf.Applications.RecentFileList.AddFile C# (CSharp) Method

AddFile() public method

Adds a file to the recent file list. If the file already exists in the list then it only changes the position in the list.
The argument fileName must not be null or empty.
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));
                }
            }
        }

Usage Example

Esempio n. 1
0
        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);
        }
All Usage Examples Of System.Waf.Applications.RecentFileList::AddFile