Click here to Skip to main content
13,003,102 members (57,477 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 4 Aug 2004

Menu handler for Most Recently Used files

, 4 Aug 2004
Rate this:
Please Sign up or sign in to vote.
A simple class for implementing Most Recently Used files functionality for multiple situations.

Sample Image - mru_files.jpg


Unlike MFC, .NET and C# application projects don't provide automatic support for Most Recently Used files. The MRU list is the last n files opened by the application, with the most recent on top. This is very handy in a .NET application, since the JIT compiler grinds away the first time a file dialog is opened, and the MRU list avoids that most of the time.

The MRU list is typically at the bottom of the File (usually the first) sub-menu, and is below a separator. This article discusses a class which maintains the menu, remembers everything between sessions, and requires minimal modification of existing code.


The need for the MRU functionality came about when the project was almost complete (last minute request by the customer). In this instance, the project had three separate windows, each loading their own file type, necessitating implementation of the MRU functionality three times. To minimize the implementation impact on tested code, and minimize the overall time required, a major goal of the project was to require the minimum interaction with existing code.

Using the code

The source code consists of a single class, MostRecent, and the file contains enough comments that the inner workings should be obvious. For each MRU menu implementation, there are four interactions with the class; three required and one optional. These interactions will be illustrated using the code supplied in the demo project.

The demo project menu has a Load item in the menu, which will load a file (preferably a small text file) and display it in the large text box. The MRU functionality will add files to the list as they are opened.

The MRU functionality is implemented through an instantiation of the MostRecent class. The constructor is used to set most of the information required:

MRUHandler = new 
    FileMnu, new Most_Recent.FileToLoad(LoadFile));


  1. DesignMRU is from an enum which specifies which window is implementing this instance. If your project only has one window, then this can be eliminated.
  2. FileMnu is the sub-menu where the MRU files will be listed.
  3. LoadFile is the function to be called when one of the MRU entries in the menu is clicked. The signature for this function must match the delegate below, declared in MostRecent.
    public delegate void FileToLoad(string fil);

Whenever the application loads a file that is not in the MRU list, this file information must be forwarded to the MostRecent object. This is done with the Add function, as shown below. Once this is done, the new file is now listed in the MRU section of the File menu.

private void LoadMnu_Click(object sender, System.EventArgs e)
    OpenFileDialog dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == DialogResult.OK)

The third mandatory implementation has already been mentioned above, and refers to the callback function used when one of the MRU items is clicked.

void LoadFile(string filPath)
        StreamReader sr = new StreamReader(filPath);
        MainTxt.Text = sr.ReadToEnd();
    catch (Exception ex)
        string err = string.Format("Unable to load {0}: {1}",
            filPath, ex.Message);

The fourth optional implementation is setting the maximum number of entries for the MRU list. As can be seen from the following snippet from MostRecent, the value can be any number greater than or equal to zero. If this functionality is not used, then the value will the default (4 in the current code).

public int MaxCount
        mxCount = value;
        if (mxCount < 0)
            mxCount = 0;
        return mxCount;

Design decisions

The MostRecent class has a GetFileName function which returns just the file title from the file path, and the title is used in the MRU section of the menu. For the original use of MostRecent, this was more than adequate, but the function can obviously be modified to return greater portions of the file path, including the entire path.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
Born-again keyboard junkie, currently designing, developing and debugging applications for the Windows environment.

You may also be interested in...


Comments and Discussions

GeneralI modified the code somewhat and this also uses user.config Pin
rickcr29-Nov-05 10:21
memberrickcr29-Nov-05 10:21 
GeneralRe: I modified the code somewhat and this also uses user.config Pin
rickcr29-Nov-05 17:01
memberrickcr29-Nov-05 17:01 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170626.1 | Last Updated 5 Aug 2004
Article Copyright 2004 by TDWiseley
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid