Build an MRU list that uses project settings in C#

[MRU list]

The post Build an MRU list in C# explains how to build an MRU list (most recently used list). That example stores recently used files in the system Registry. This example does the same thing but it stores file information in the program’s settings. It stores the file paths as a single semi-color delimited string in the project settings.


To create the setting, open the Project menu and select Settings from the bottom. On the Settings tab, create a new setting named FilePaths as shown in the following picture.


[MRU list]

Now the program can use that setting. Unfortunately it’s the form’s designer-generated code that defines the setting. The previous example uses the MruList class to manage its MRU list, so the MruList object that manages the list cannot easily use the form’s settings.

To make using the setting easy, I added the following methods to the program’s Form1 class.

// Methods to get and save the MRU list file paths.
public string GetMruFilePaths()
{
    return Properties.Settings.Default.FilePaths;
}

public void SaveMruFilePaths(string paths)
{
    Properties.Settings.Default.FilePaths = paths;
    Properties.Settings.Default.Save();
}

The GetMruFilePaths method simply returns the FilePaths setting. The SaveMruFilePaths method sets the FilePaths value and then saves it.

The new version of the MruList class now needs an instance of the Form1 class so it can use these two new methods. The following code shows how the new MruList class begins.

public class MruList
{
    // The application's form.
    private Form1 TheForm = null;
    ...
    // Constructor.
    public MruList(Form1 the_form, ToolStripMenuItem menu,
        int num_files)
    {
        TheForm = the_form;
        ...
    }
    ...
}

The class now defines a new field TheForm to hold the instance of the form that is using the MruList object. The class’s constructor saves the form instance in that field.

The rest of the MruList class works much as it did before except it uses the new setting instead of the Registry. The following code shows the new version of the LoadFiles method.

// Load saved items from the project settings.
private void LoadFiles()
{
    // Get the file paths from the project settings.
    string all_paths = TheForm.GetMruFilePaths();
    string[] separators = { ";" };
    foreach (string path in all_paths.Split(
        separators, StringSplitOptions.RemoveEmptyEntries))
            FileInfos.Add(new FileInfo(path));
}

This version uses the form’s GetMruFilePaths to get the file path string. It splits the semi-colon delimited file paths, uses them to create FileInfo objects, and adds the objects to the FileInfos list.

The following code shows the new version of the SaveFiles method.

// Save the current items in the settings.
private void SaveFiles()
{
    // Save the current entries.
    string all_paths = "";
    foreach (FileInfo file_info in FileInfos)
        all_paths += file_info.FullName + ";";
    TheForm.SaveMruFilePaths(all_paths);
}

This method loops through the FileInfos entries and adds their full names (including paths) to a semi-colon delimited string. After building the string, the method calls the form’s SaveMruFilePaths method to update the setting.

Download the example and see the previous example to see additional details.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in files, programs, registry and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.