有关最近使用的文件的帮助(MRU) [英] Help about most recently used file(MRU)

查看:106
本文介绍了有关最近使用的文件的帮助(MRU)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要mru的代码,即c#中最近使用的文件.
即当我单击最近使用的文件标签时,它应列出所有
的文件 我最近打开或使用过.

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.

推荐答案

Google是您的朋友:很好,经常拜访他.与在这里发布问题相比,他可以更快地回答问题.

从以下站点尝试: C#中最近使用过的(MRU)菜单类 [
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#[^]


有几个可能性.我将使用应用程序设置进行解释.

首先,在解决方案资源管理器中,右键单击您的项目,然后选择属性,然后选择设置标签.
名称列中,输入Mru
类型列中,选择System.Collections.Specialized.StringCollection
范围列中,选择User
值列中,单击小按钮以编辑集合,输入任何值,然后单击确定,然后再次单击按钮,删除值,然后再次单击确定以使集合为空.没有这个技巧,该集合将不会被实例化,并且您将有一个异常将运行您的代码.

好的,您已经创建了MRU列表,它将保存在您的应用程序设置文件中.

现在让我们在表单中添加一些代码.我想您已经创建了一个包含两个条目的菜单:OpenRecent files.

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.

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)
    {
        ...
    }
}


这篇关于有关最近使用的文件的帮助(MRU)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆