How do I create a MRU in V 2010Express C# in the simplest way?

First of all I am a newbie in C# Programming, and I need to create a simple MRU as fast as i could.

Well the thing is I've tried looking at some online examples but however I found them to be quite a bit too confusing...

So is there anyway that anyone can create a "Recently Used" section in the toolstripmenuitem without going into those complicated codes??

E.g I will not be able to understand this stuff...

Registry key:



Application.UserAppDataRegistry.DeleteSubKey("MRU", false); RegistryKey appKey = Application.UserAppDataRegistry.CreateSubKey("MRU"); dictionary microsoft.win32

I will only need something as simple as shown in this link below

-------------Problems Reply------------

So you want to create a submenu like in the screenshot? For this, you will have to:

  • Store the list of recently-used files somewhere. This could be the registry, or it could just be a simple textfile, which I’ll do now to keep it simple.
  • Learn how to generate menu items at runtime instead of in the designer.

1. Store the MRU in a file

You will probably have already declared a private field to contain your MRU, right?

private List<string> _mru = new List<string>();

Every time someone opens a file, you add this file to the beginning of the MRU, right?

_mru.Insert(0, fullFilePath);

Now, of course when the application closes, you need to save this MRU to a file. Let’s do that in the Form’s FormClosed event. Double-click the FormClosed event in the properties and write some code which looks somewhat like this:

var appDataPath = Application.UserAppDataPath;
var myAppDataPath = Path.Combine(appDataPath, "MyApplication");
var mruFilePath = Path.Combine(myAppDataPath, "MRU.txt");
File.WriteAllLines(mruFilePath, _mru);

Now we have saved the MRU in a file. Now obviously when the application starts, we need to load it again, so do something like this in the form’s Load event:

var appDataPath = Application.UserAppDataPath;
var myAppDataPath = Path.Combine(appDataPath, "MyApplication");
var mruFilePath = Path.Combine(myAppDataPath, "MRU.txt");
if (File.Exists(mruFilePath))

2. Create the menu items

Now that _mru contains the file paths that we want in our menu, we need to create a new menu item for each. I’ll be assuming here that you already have a menu item in the File menu (the item called “Most Recently Used” in your screenshot) and that it is called mnuRecentlyUsed, and that we only need to create sub-items:

foreach (var path in _mru)
var item = new ToolStripMenuItem(path);
item.Tag = path;
item.Click += OpenRecentFile;

Now all we need is the method that actually opens a file, which I called OpenRecentFile:

void OpenRecentFile(object sender, EventArgs e)
var menuItem = (ToolStripMenuItem) sender;
var filepath = (string) menuItem.Tag;

// Proceed to open the file
// ...


Please don’t use any of this code unless you understand it and you are sure that it is written to do what you intended. If it needs to do something slightly different, I’m sure you can make the necessary changes yourself.

Also, I’m sure you will have noticed that the above doesn’t update the sub-menu while the program is running. If you understand how the above code works, then I’m sure you’ll be able to figure out the rest for yourself.

This project does exactly what you want

Category:c# Views:0 Time:2010-08-20

Related post

  • Dynamically manipulate a menu to create an MRU in a Windows app in Progress 2012-02-07

    I have a Windows application written in Progress. I'm working with version 10.1C. I would like to add MRU functionality to the menu, i.e. I want to add, remove and modify menu items in the application's File menu, to show the user's most recent files

  • What is the easiest and most compact way to create a IEnumerable or ICollection? 2009-09-26

    So, many times we have a function that accepts an IEnumerable or ICollection as a parameter. In cases where we have single items, but no collection to hold them, we must create a collection before passing them to the function, like: T o1, o2, o3; Foo

  • What are the exact steps for creating and then linking against a Win32 DLL on the command line? 2012-01-27

    I apologize if this question seems overtly simple, but I'm very new to Win32 programming and I couldn't get this to work after some time with Google and passing different options to cl.exe. I'm asking how to do this on the command line for two reason

  • Which Java collection should I use to implement a thread-safe cache? 2010-07-28

    I'm looking to implement a simple cache without doing too much work (naturally). It seems to me that one of the standard Java collections ought to suffice, with a little extra work. Specifically, I'm storing responses from a server, and the keys can

  • I have Microsoft Office for students installed in my laptop and desktop. I can open Word document on my desktop, but not on my laptop. It keep saying that file is not available. What to do? 2013-03-20

    I have troubles open files on my laptop. Windows claiming that file such or such is not available. But I was able to open the same documents on my second computer. Both computers have Microsoft Office for students installed. If anybody could explain

  • How can I restore Word 2010 recent documents list after windows 7 reinstallation 2015-02-20

    Hi guys I've just reinstalled my Win 7 OS purely to enhance speed & consider myself a fairly competent PC user so comfortable moving things around, and restoring folders etc. As always I backed up everything anyway, but do have the Windows.old fo

  • How to do private comms between private apps over network? 2008-12-24

    It's basically one app that is installed on multiple PC's, each install maintaining it's own database which is sync'd with other's as & when they are up (connected to the same network) at the same time. I've tested this using simple socket connec

  • How can I prevent the VS WPF designer putting margins on every object from the toolbox 2009-01-30

    When I drag a control from the toolbox on to the Visual Studio 2008 WPF design surface, it sets a margin based on where I dropped it and a default size. Is there any way to prevent VS setting those? When I drop a control on a stackpanel I want it to

  • Can I add a new folder to a TFS repository server-side without using a local working copy? 2009-03-13

    I'm currently transfering some source code from a Subversion (SVN) repository to my client's Team Foundation Server. One of the things I stumbled upon is that there seems to be no way to edit the repository without first pulling all files in that rep

  • ReverseString, a C# interview-question 2009-06-17

    I had an interview question that asked me for my 'feedback' on a piece of code a junior programmer wrote. They hinted there may be a problem and said it will be used heavily on large strings. public string ReverseString(string sz) { string result = s

  • schtasks.exe vs ITaskScheduler 2009-09-25

    I'm trying to write some code to automatically "configure" a bunch of scheduled tasks on a machine. The program will read an XML file then "configure" the run times etc. of the task based on the information in the XML file. The idea should be that th

  • How to prepopulate ID in Django 2009-10-31

    I have simple question model : class Question(Polymorph): text = models.CharField(max_length=256) poll = models.ForeignKey(Poll) index = models.IntegerField() And I would like to prepopulate ( when saving ) index field with ID value. Of course before

  • Have you ever restricted yourself to using a subset of language features? 2009-12-08

    Have you ever restricted yourself to using a subset of language features, and more importantly, why? I'm curious to find out who choose to use only certain language features and avoid others in order to win big in areas such as, but not limited to, m

  • Coder collaboration best practices using Git 2010-01-27

    I'm having some troubles in understanding Git team-working principles. Consider a team of two programmers: A and B. They are working on a Project. Also, there is a remote server with a repo on it. A and B are collaborating remotely. There are some co

  • "Cannot find executable for CFBundle/CFPlugIn" error 2010-02-26

    Cannot find executable for CFBundle/CFPlugIn 0x432bfa0 (not loaded) Cannot find function pointer NewPlugIn for factory C5A4CE5B-0BB8-11D8-9D75-0003939615B6 in CFBundle/CFPlugIn 0x432bfa0 (not loaded) That's the error I get when I try to run this code

  • File from msi location 2010-03-01

    I would like to copy file that placed near the msi to the program file folder. I have the next code in my project: <DirectoryRef Id="Global.txt"> <Component Id="Global.txt" Guid="263B6F39-BE6B-4b51-8AE4-4D152436F0AE" DiskId="2" NeverOverwrit

  • export to epub format in php 2010-07-23

    Does anyone know of a php class that can create/export ePub files? I have a book stored in a mysql database and I wish to publish it as an ePub format book? It's a very basic book, simple text and chapters, and a few images. Any suggestions? --------

  • Can't bind to IEnumerable implementation using yield 2010-08-18

    I have a class encapsulating a bunch of collections. I would like to bind this class to a listbox to display the items in the collections. The class implements IEnumerable. When the listbox is displayed I am expecting the IEnumerable.GetEnumerator me

  • PowerShell to Open Outlook, Make Visible 2010-09-09

    My mission is to graduate from using PowerShell to create an instance of Outlook to simply viewing, or making visible the process that I can see in the TaskManager. To Digress, this works for Word, but not if I substitute Outlook. for Word. $MsApp =

Copyright (C), All Rights Reserved.

processed in 0.168 (s). 11 q(s)