[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Build an MRU list that uses project settings in C#

[Build an MRU list that uses project settings in C#]

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.

[Build an MRU list that uses project settings in C#]

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 to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.