none
how to make a list of recent files in a form in vb.net RRS feed

  • Question

  • hi ,  i am just wondering on how can i make a list of recent file opened on my program in vb.net. The recent files  filenames must be in a linklabel in a form and in the menustrip.. hope you can help me ..

    advance thank you

    Sunday, July 24, 2011 8:56 AM

Answers

  • Dear Mymy,

    I completely agree with Thorsten. I build you a frame, instead of using XML i used a blunt text file, but xml will indeed give you more flexibility. Instead of saving it to localAppData i saved the text file in the local directory. Which ever you prefer, ... the localappdata location as suggested by Thorsten is definitely the best spot as it will allow you to have a recent file list for all users of your app on that workstation.

     

     

    Imports System.IO
    Public Class Form1
      Public recentItems As String
      Public recentFiles(10) As String
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
          'Load Settings
          recentItems = Path.Combine(CurDir(), "recent.dat")
          Dim itm As Integer = 0
          If File.Exists(recentItems) Then
            Using sr As New StreamReader(recentItems)
              While Not sr.EndOfStream And itm < 10
                recentFiles(itm) = sr.ReadLine()
                itm += 1
              End While
            End Using
    
            Dim maxRecentFiles As Integer = 1
            'I chose to hardcode a maximum of 10 items. Of course you can do this 
            'differently. But anybody can use / adjust the example below ;)
            For Each recentFile In recentFiles
              Select Case maxRecentFiles
                Case Is = 1
                  fl1.Enabled = True
                  fl1.Visible = True
                  fl1.Text = "01." & recentFile
                Case Is = 2
                  fl2.Enabled = True
                  fl2.Visible = True
                  fl2.Text = "02." & recentFile
                Case Is = 3
                  fl3.Enabled = True
                  fl3.Visible = True
                  fl3.Text = "03." & recentFile
                Case Is = 4
                  fl4.Enabled = True
                  fl4.Visible = True
                  fl4.Text = "04." & recentFile
                Case Is = 5
                  fl5.Enabled = True
                  fl5.Visible = True
                  fl5.Text = "05." & recentFile
                Case Is = 6
                  fl6.Enabled = True
                  fl6.Visible = True
                  fl6.Text = "06." & recentFile
                Case Is = 7
                  fl7.Enabled = True
                  fl7.Visible = True
                  fl7.Text = "07." & recentFile
                Case Is = 8
                  fl8.Enabled = True
                  fl8.Visible = True
                  fl8.Text = "08." & recentFile
                Case Is = 9
                  fl9.Enabled = True
                  fl9.Visible = True
                  fl9.Text = "09." & recentFile
                Case Is = 10
                  fl10.Enabled = True
                  fl10.Visible = True
                  fl10.Text = "10." & recentFile
                Case Else
                  'Right now the log of recentfiles will grow endlessly
                  'but it will always only show the last 10 items.
                  'make this pretty and clear / clean up the older items
              End Select
              maxRecentFiles += 1
            Next
          Else
            'If the file was not found, there were no recent files.
            'disable all items in the submenu
            For Each menuItm As ToolStripMenuItem In Me.btnRecent.DropDownItems
              With menuItm
                .Enabled = False
                .Visible = False
              End With
            Next
          End If
          TheSubThatLoadsYourFile()
        Catch ex As Exception
    
        End Try
      End Sub
    
      Private Sub TheSubThatLoadsYourFile()
        Try
          'I suppose your app has got a sub somewhere that loads a file
          'onto a form
          'Extend this sub so that it will save this to your recent file -
          'activity list
          Dim yourLatestFile As String = "C:\temp\example12.txt"
          '^replace with the name of the latest file you opened
    
          Dim allYourLatestFiles As String = ""
    
          If File.Exists(recentItems) Then
            Using sr As New StreamReader(recentItems)
              allYourLatestFiles = sr.ReadToEnd
            End Using
          End If
    
          Using sw As New StreamWriter(recentItems, False)
            '^do not append but overwrite if the item already exists
            sw.WriteLine(yourLatestFile)
            '^the last opened file will be the first in the list
            sw.Write(allYourLatestFiles)
          End Using
    
        Catch ex As Exception
          Debug.Print(ex.ToString)
        End Try
      End Sub
    
    
    End Class
    
    Hope this helps,

    Cheers,

    John

     

    Sunday, July 24, 2011 11:21 AM
  • Track the opened file names since the first launch of your program in a readable text file using StreamWriter/Reader classes. While your app is open for the current session, for example when user opens a file write the file name, add it to file:

     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Using sw As New IO.StreamWriter("c:\yourfolder\recent_list.txt", True)
                    sw.WriteLine(OpenFileDialog1.FileName)
                End Using
    End If

    In that case, the last added filename will be the most recent one, so when you want to retrive the most recent one you'll use that function:

     Private Function recentList() As String()
            Dim content() As String = System.IO.File.ReadAllLines("c:\your_folder\recent_list.txt")
            Array.Reverse(content)
            Return content
        End Function

    Using linklabel or menustrip doesn't make much difference as they're just used for representation of data.

    HTH.

     


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    Sunday, July 24, 2011 11:23 AM

All replies

  • Hi,

    either write to the registry as many programs do, or, what I'd do is to save the file-information in a xml-file (which allows to add more information and therefore build a more flexible recent-docs-list). Save it to a directory under LocalAppData. Load the xml file at startup and extract the needed data, check if the files still exists on the filesystem etc.

    Or maybe save the filenames to your application-settings.

    Regards,

      Thorsten



    Sunday, July 24, 2011 11:14 AM
  • Dear Mymy,

    I completely agree with Thorsten. I build you a frame, instead of using XML i used a blunt text file, but xml will indeed give you more flexibility. Instead of saving it to localAppData i saved the text file in the local directory. Which ever you prefer, ... the localappdata location as suggested by Thorsten is definitely the best spot as it will allow you to have a recent file list for all users of your app on that workstation.

     

     

    Imports System.IO
    Public Class Form1
      Public recentItems As String
      Public recentFiles(10) As String
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
          'Load Settings
          recentItems = Path.Combine(CurDir(), "recent.dat")
          Dim itm As Integer = 0
          If File.Exists(recentItems) Then
            Using sr As New StreamReader(recentItems)
              While Not sr.EndOfStream And itm < 10
                recentFiles(itm) = sr.ReadLine()
                itm += 1
              End While
            End Using
    
            Dim maxRecentFiles As Integer = 1
            'I chose to hardcode a maximum of 10 items. Of course you can do this 
            'differently. But anybody can use / adjust the example below ;)
            For Each recentFile In recentFiles
              Select Case maxRecentFiles
                Case Is = 1
                  fl1.Enabled = True
                  fl1.Visible = True
                  fl1.Text = "01." & recentFile
                Case Is = 2
                  fl2.Enabled = True
                  fl2.Visible = True
                  fl2.Text = "02." & recentFile
                Case Is = 3
                  fl3.Enabled = True
                  fl3.Visible = True
                  fl3.Text = "03." & recentFile
                Case Is = 4
                  fl4.Enabled = True
                  fl4.Visible = True
                  fl4.Text = "04." & recentFile
                Case Is = 5
                  fl5.Enabled = True
                  fl5.Visible = True
                  fl5.Text = "05." & recentFile
                Case Is = 6
                  fl6.Enabled = True
                  fl6.Visible = True
                  fl6.Text = "06." & recentFile
                Case Is = 7
                  fl7.Enabled = True
                  fl7.Visible = True
                  fl7.Text = "07." & recentFile
                Case Is = 8
                  fl8.Enabled = True
                  fl8.Visible = True
                  fl8.Text = "08." & recentFile
                Case Is = 9
                  fl9.Enabled = True
                  fl9.Visible = True
                  fl9.Text = "09." & recentFile
                Case Is = 10
                  fl10.Enabled = True
                  fl10.Visible = True
                  fl10.Text = "10." & recentFile
                Case Else
                  'Right now the log of recentfiles will grow endlessly
                  'but it will always only show the last 10 items.
                  'make this pretty and clear / clean up the older items
              End Select
              maxRecentFiles += 1
            Next
          Else
            'If the file was not found, there were no recent files.
            'disable all items in the submenu
            For Each menuItm As ToolStripMenuItem In Me.btnRecent.DropDownItems
              With menuItm
                .Enabled = False
                .Visible = False
              End With
            Next
          End If
          TheSubThatLoadsYourFile()
        Catch ex As Exception
    
        End Try
      End Sub
    
      Private Sub TheSubThatLoadsYourFile()
        Try
          'I suppose your app has got a sub somewhere that loads a file
          'onto a form
          'Extend this sub so that it will save this to your recent file -
          'activity list
          Dim yourLatestFile As String = "C:\temp\example12.txt"
          '^replace with the name of the latest file you opened
    
          Dim allYourLatestFiles As String = ""
    
          If File.Exists(recentItems) Then
            Using sr As New StreamReader(recentItems)
              allYourLatestFiles = sr.ReadToEnd
            End Using
          End If
    
          Using sw As New StreamWriter(recentItems, False)
            '^do not append but overwrite if the item already exists
            sw.WriteLine(yourLatestFile)
            '^the last opened file will be the first in the list
            sw.Write(allYourLatestFiles)
          End Using
    
        Catch ex As Exception
          Debug.Print(ex.ToString)
        End Try
      End Sub
    
    
    End Class
    
    Hope this helps,

    Cheers,

    John

     

    Sunday, July 24, 2011 11:21 AM
  • Track the opened file names since the first launch of your program in a readable text file using StreamWriter/Reader classes. While your app is open for the current session, for example when user opens a file write the file name, add it to file:

     If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Using sw As New IO.StreamWriter("c:\yourfolder\recent_list.txt", True)
                    sw.WriteLine(OpenFileDialog1.FileName)
                End Using
    End If

    In that case, the last added filename will be the most recent one, so when you want to retrive the most recent one you'll use that function:

     Private Function recentList() As String()
            Dim content() As String = System.IO.File.ReadAllLines("c:\your_folder\recent_list.txt")
            Array.Reverse(content)
            Return content
        End Function

    Using linklabel or menustrip doesn't make much difference as they're just used for representation of data.

    HTH.

     


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    Sunday, July 24, 2011 11:23 AM
  • Select Case maxRecentFiles
          Case Is = 1
           fl1.Enabled = True
           fl1.Visible = True
           fl1.Text = "01." & recentFile
          Case Is = 2
           fl2.Enabled = True
           fl2.Visible = True
           fl2.Text = "02." & recentFile
          Case Is = 3
           fl3.Enabled = True
           fl3.Visible = True
           fl3.Text = "03." & recentFile
          Case Is = 4
           fl4.Enabled = True
           fl4.Visible = True
           fl4.Text = "04." & recentFile
          Case Is = 5
           fl5.Enabled = True
           fl5.Visible = True
           fl5.Text = "05." & recentFile
          Case Is = 6
           fl6.Enabled = True
           fl6.Visible = True
           fl6.Text = "06." & recentFile
          Case Is = 7
           fl7.Enabled = True
           fl7.Visible = True
           fl7.Text = "07." & recentFile
          Case Is = 8
           fl8.Enabled = True
           fl8.Visible = True
           fl8.Text = "08." & recentFile
          Case Is = 9
           fl9.Enabled = True
           fl9.Visible = True
           fl9.Text = "09." & recentFile
          Case Is = 10
           fl10.Enabled = True
           fl10.Visible = True
           fl10.Text = "10." & recentFile
          Case Else
           'Right now the log of recentfiles will grow endlessly
           'but it will always only show the last 10 items.
           'make this pretty and clear / clean up the older items
         End Select
    
    the fl variable is an error. 
     
    Sunday, August 7, 2011 9:58 AM
  • ...
    the fl variable is an error. 
     

    Hi,

    actually there are 10 fl-variables (fl1 to fl10). These seem to be the MenuItems you define for your recent filelist (so the name fl refers to the word FileList, IMHO). Change these variable-names to the MenuItems you define for your recent-files.

    Regards,

      Thorsten

    Sunday, August 7, 2011 11:46 AM