vb.netrecent-file-list

location of recent files list vb.net


I am looking to add a recent files list to an application I am writing. I was thinking of adding the recent files to an xml file.

Where should this file be stored? And how should it be called from the code?

I would imagine the xml would be stored in the same folder that the application is installed in, but not everybody will install the application in the same directory.

Is there a way to code it in such a manner that it will always be stored in the same folder as the application will be installed in?

much thanks in advance!


Solution

  • Here is an example using My.Settings. It requires you to open the Settings page of the project properties and add a setting of type StringCollection named RecentFiles as well as a ToolStripMenuItem with the text "Recent".

    Imports System.Collections.Specialized
    
    Public Class Form1
    
        Private Const MAX_RECENT_FILES As Integer = 10
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            LoadRecentFiles()
        End Sub
    
        Private Sub LoadRecentFiles()
            Dim recentFiles = My.Settings.RecentFiles
    
            'A StringCollection setting will be Nothing by default, unless you edit it in the Settings designer.
            If recentFiles Is Nothing Then
                My.Settings.RecentFiles = New StringCollection()
                recentFiles = My.Settings.RecentFiles
            End If
    
            'Get rid of any existing menu items.
            RecentToolStripMenuItem.DropDownItems.Clear()
    
            'Add a menu item for each recent file.
            If recentFiles.Count > 0 Then
                RecentToolStripMenuItem.DropDownItems.AddRange(recentFiles.Cast(Of String)().
                                                                           Select(Function(filePath) New ToolStripMenuItem(filePath,
                                                                                                                           Nothing,
                                                                                                                           AddressOf RecentFileMenuItems_Click)).
                                                                           ToArray())
            End If
        End Sub
    
        Private Sub UpdateRecentFiles(filePath As String)
            Dim recentFiles = My.Settings.RecentFiles
    
            'If the specified file is already in the list, remove it from its old position.
            If recentFiles.Contains(filePath) Then
                recentFiles.Remove(filePath)
            End If
    
            'Add the new file at the top of the list.
            recentFiles.Insert(0, filePath)
    
            'Trim the list if it is too long.
            While recentFiles.Count > MAX_RECENT_FILES
                recentFiles.RemoveAt(MAX_RECENT_FILES)
            End While
    
            LoadRecentFiles()
        End Sub
    
        Private Sub RecentFileMenuItems_Click(sender As Object, e As EventArgs)
            Dim menuItem = DirectCast(sender, ToolStripMenuItem)
            Dim filePath = menuItem.Text
    
            'Open the file using filePath here.
        End Sub
    
    End Class
    

    Note that the Load event handler includes a bit of code to allow for the fact that a setting of type StringCollection will be Nothing until you assign something to it. If you want to avoid having to do that in code, do the following.

    1. After adding the setting, click the Value field and click the button with the ellipsis (...) to edit.
    2. Add any text to the editor and click OK. Notice that some XML has been added that includes the item(s) you added.
    3. Click the edit button (...) again and delete the added item(s). Notice that the XML remains but your item(s) is gone.

    That XML code will cause a StringCollection object to be created when the settings are first loaded, so there's no need for you to create one in code.

    EDIT:

    I tested that by adding the following code:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using dialogue As New OpenFileDialog
            If dialogue.ShowDialog() = DialogResult.OK Then
                UpdateRecentFiles(dialogue.FileName)
            End If
        End Using
    End Sub
    

    I was able to add ten files to the list via that Button and then they started dropping off the end of the list as I added more. If I re-added one that was already in the list, it moved to the top. If I closed the app and ran it again, the list persisted.