Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i need code for mru i.e. most recently used file in c#.
i.e when i click on most recently used file tag, it should list all file that
i had recently open, or used.
Posted
Updated 4-Mar-11 3:18am
v2
Comments
Albin Abel 4-Mar-11 6:22am    
is it for your application? then when a file open store it in application settings. Up to 5 numbers may be
Sumit Memane 4-Mar-11 6:26am    
i am new in c#, i dont know any thing about C#,
but i need it in my application,
Toli Cuturicu 4-Mar-11 9:19am    
If you are new in c#, then you should try easier stuff first.

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

Try from this very site: Most Recently Used (MRU) Menu Class in C#[^]
 
Share this answer
 
Comments
Sumit Memane 4-Mar-11 6:32am    
Frnd, this is not working properly
i had tried it, before sending my query
OriginalGriff 4-Mar-11 6:34am    
Then ask the author directly: there is a "New Message" button at the bottom of the article.
Sumit Memane 4-Mar-11 6:40am    
DO you have any another code
There are several possibilities. I will explain one using Application Settings.

First, in the solution explorer, right-click on your project, then choose properties, and select the Settings tab.
In the Name column, enter Mru
In the Type column, select System.Collections.Specialized.StringCollection
In the Scope column, select User
In the Value column, click on the small button to edit the collection, enter any value, then click on OK, then click again on the button, remove the value and click OK again to make the collection empty. Without this trick, the collection wouldn't be instanciated and you would have an exception will running your code.

OK you have created your MRU list and it will be saved in your application settings file.

Now let's add some code in your form. I suppose you already created a menu with two entries: Open and Recent files.

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        //fill the menu items
        UpdateMRUMenu();
        //this event will be fired if the user selects a file in the MRU
        recentFilesToolStripMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(recentFilesToolStripMenuItem_DropDownItemClicked);
    }

    //user clicked on File, Recent files
    void recentFilesToolStripMenuItem_DropDownItemClicked(object sender,
        ToolStripItemClickedEventArgs e)
    {
        //the user clicked on a file in the MRU. Let's open it.
        OpenFile(e.ClickedItem.Text);
    }

    // updates the menu items
    private void UpdateMRUMenu()
    {
        recentFilesToolStripMenuItem.DropDownItems.Clear();
        foreach (string path in Properties.Settings.Default.Mru)
            recentFilesToolStripMenuItem.DropDownItems.Add(path);
    }

    // add this file to the MRU
    void AddToMRU(string path)
    {
        //add the file to the list
        Properties.Settings.Default.Mru.Insert(0, path);
        //just keep the 5 recentest elements
        while (Properties.Settings.Default.Mru.Count > 5)
            Properties.Settings.Default.Mru.RemoveAt(Properties.Settings.Default.Mru.Count - 1);
        //save the settings file
        Properties.Settings.Default.Save();
        //update the menu items
        UpdateMRUMenu();
    }

    //user clicked on File, Open
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            string path = dlg.FileName;
            //add this file to the MRU
            AddToMRU(path);
            //open the file
            OpenFile(path);
        }
    }

    //open your file here
    void OpenFile(string path)
    {
        ...
    }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 12:36pm    
Very good, my 5.
--SA
freezizo84 21-Aug-11 20:08pm    
How can i open these files when i click on them ?
freezizo84 21-Aug-11 20:10pm    
how can i change the number of shows items in the recent files
Olivier Levrey 26-Aug-11 16:54pm    
All the code is in my answer:
1- in the Form1 constructor, I handle DropDownItemClicked event to know when the user clicks on of the files: Just add your own code in the OpenFile method.
2- In the AddToMRU method, I hard coded 5 as the maximum number of files to keep. Just change this value to whatever you want.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900