Click here to Skip to main content
Click here to Skip to main content

Tagged as

Create a Recent File List Menu and Save to File

, 2 Jan 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Creating a recent file list and saving it to a configuration-like file


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
    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 ) 
    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
    //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
      //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
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{ ...
   //insert new item after open a file

So that's it -simple but useful code.


  • 2nd January, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Engineer Donatus Inc.
Indonesia Indonesia
I'm currently a student of Brawijaya University. I work on programming just for hobby. I learn programming since 2nd year of my college. That's when I first bought my PC on year 2004.

Comments and Discussions

Generalgood job thanks Pinmembermatafill++c5-Nov-12 1:37 
GeneralA very nice article Pinmemberzippy19812-Jan-09 6:28 
GeneralRe: A very nice article Pinmemberasugix5-Jan-09 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 2 Jan 2009
Article Copyright 2009 by asugix
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid