Click here to Skip to main content
15,879,474 members
Articles / Programming Languages / C#

Create a Recent File List Menu and Save to File

Rate me:
Please Sign up or sign in to vote.
3.80/5 (7 votes)
2 Jan 2009CPOL 56.4K   1.9K   21   4
Creating a recent file list and saving it to a configuration-like file
recentlyList

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

C#
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

C#
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

C#
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:

C#
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

License

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


Written By
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

 
PraiseSimple and works fine Pin
betojasz29-Jun-16 6:55
betojasz29-Jun-16 6:55 
Generalgood job thanks Pin
matafill++c5-Nov-12 0:37
matafill++c5-Nov-12 0:37 
good job thanks !
GeneralA very nice article Pin
zippy19812-Jan-09 5:28
zippy19812-Jan-09 5:28 
GeneralRe: A very nice article Pin
asugix5-Jan-09 2:10
asugix5-Jan-09 2:10 

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

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