Create a Recent File List Menu and Save to File
Creating a recent file list and saving it to a configuration-like file

Introduction
When creating an application, sometimes it's useful if you want to create a recent file list as a shortcut to open a previously-opened file. This makes opening a file look simple and handy.
Using the Code
To create a recent file list, we need just three methods:
- Save file list
This method is called whenever you want to add an item to a list and save it. Usually, this item is a path to a previously-opened file.
- Load file list
This method is to load a list from file but doesn't insert it to menu. Usually, this method is called at
form_load
and whenever you want to refresh list and list menu. - Recent file click
This is a click handler when user clicks the menus. Usually, this reopens a file.
The Code
Save File List
private void SaveRecentFile(string path)
{
//clear all recent list from menu
recentToolStripMenuItem.DropDownItems.Clear();
LoadRecentList(); //load list from file
if (!(MRUlist.Contains(path))) //prevent duplication on recent list
MRUlist.Enqueue(path); //insert given path into list
//keep list number not exceeded the given value
while (MRUlist.Count > MRUnumber )
{
MRUlist.Dequeue();
}
foreach (string item in MRUlist )
{
//create new menu for each item in list
ToolStripMenuItem fileRecent = new ToolStripMenuItem
(item, null, RecentFile_click);
//add the menu to "recent" menu
recentToolStripMenuItem.DropDownItems.Add(fileRecent);
}
//writing menu list to file
//create file called "Recent.txt" located on app folder
StreamWriter stringToWrite =
new StreamWriter(System.Environment.CurrentDirectory + "\\Recent.txt");
foreach (string item in MRUlist )
{
stringToWrite.WriteLine(item); //write list to stream
}
stringToWrite.Flush(); //write stream to file
stringToWrite.Close(); //close the stream and reclaim memory
}
Load File List
private void LoadRecentList()
{//try to load file. If file isn't found, do nothing
MRUlist.Clear();
try
{
//read file stream
StreamReader listToRead =
new StreamReader(System.Environment.CurrentDirectory + "\\Recent.txt");
string line;
while ((line = listToRead.ReadLine()) != null) //read each line until end of file
MRUlist.Enqueue(line); //insert to list
listToRead.Close(); //close the stream
}
catch (Exception){}
}
Recent File Click
private void RecentFile_click(object sender, EventArgs e)
{
//just load a file
richTextBox1.LoadFile(sender.ToString (), RichTextBoxStreamType.PlainText);
}
And here's where those methods are called:
private void Form1_Load(object sender, EventArgs e)
{
LoadRecentList(); //load a configuration-like file containing recent list
foreach (string item in MRUlist )
{
//populating menu
ToolStripMenuItem fileRecent =
new ToolStripMenuItem(item, null, RecentFile_click);
//add the menu to "recent" menu
recentToolStripMenuItem.DropDownItems.Add(fileRecent);
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{ ...
//insert new item after open a file
SaveRecentFile(openFileDialog1.FileName);
...
}
So that's it -simple but useful code.
History
- 2nd January, 2009: Initial post