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

Create a Recent File List Menu and Save to File

By , 2 Jan 2009
 
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

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

License

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

About the Author

asugix
Engineer Donatus Inc.
Indonesia Indonesia
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalgood job thanksmembermatafill++c5 Nov '12 - 0:37 
GeneralA very nice articlememberzippy19812 Jan '09 - 5:28 
GeneralRe: A very nice articlememberasugix5 Jan '09 - 2:10 

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

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