0

我希望将最近的文件列表添加到我正在编写的应用程序中。我正在考虑将最近的文件添加到 xml 文件中。

这个文件应该存储在哪里?应该如何从代码中调用它?

我想 xml 将存储在安装应用程序的同一文件夹中,但不是每个人都会将应用程序安装在同一目录中。

有没有办法对其进行编码,使其始终存储在与安装应用程序相同的文件夹中?

非常感谢提前!

4

1 回答 1

2

这是一个使用My.Settings. 它要求您打开项目属性的设置StringCollection页面并添加一个名为的设置RecentFiles以及ToolStripMenuItem带有文本“最近”的设置。

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

请注意,Load事件处理程序包含一些代码,以允许在您为其分配某些内容之前设置StringCollection类型Nothing。如果您想避免在代码中这样做,请执行以下操作。

  1. 添加设置后,单击Value字段并单击带有省略号 (...) 的按钮进行编辑。
  2. 将任何文本添加到编辑器并单击OK。请注意,已添加一些 XML,其中包括您添加的项目。
  3. 再次单击编辑按钮 (...) 并删除添加的项目。请注意,XML 仍然存在,但您的项目已消失。

该 XML 代码将导致StringCollection在首次加载设置时创建一个对象,因此您无需在代码中创建一个对象。

编辑:

我通过添加以下代码进行了测试:

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

我可以通过它将十个文件添加到列表中Button,然后随着我添加更多文件,它们开始从列表的末尾删除。如果我重新添加一个已经在列表中的,它会移到顶部。如果我关闭应用程序并再次运行它,列表仍然存在。

于 2020-04-10T08:10:05.397 回答