PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Recent file list troubles-VBForums
Results 1 to 9 of 9

Thread: Recent file list troubles

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Recent file list troubles

    I am working on a program where I have an open recent file list option.

    The trouble I am having is adding the most recently opened file to the list.

    I tried adding the path to the most recently opened file to the text file where the list is stored, then reading the entire contents back in to an array and adding it to the menu. I would then retrieve the entire contents to make sure that the most recently opened file was not already in the list and then write the contents back to the file. I did this with the following code:
    Code:
    Dim recentStreamWriter As New IO.StreamWriter(RecentFile, True)    'Stream writer to add the most recently opened file to Recent files list (Recent.txt)
    recentStreamWriter.WriteLine(pathToXML)                                     'Add most recent file to Recent.txt
    recentStreamWriter.Close()
    
    Dim RecentStreamReader As New IO.StreamReader(RecentFile)         'Stream reader to retrieve the entire list of recent files including most recently added file
    Dim currentFileContents = File.ReadAllLines(RecentFile                     'Get the list of recently opend files from text file
    Dim uniqueratingFiles = currentFileContents.Distinct()                     'Make sure their are only unigue items, no duplicates
    RecentStreamReader.Close()                                                      'Close the file for reading
    
    Dim uniquerecentStreamWriter As New IO.StreamWriter(RecentFile, False)    'Stream reader to write the list of unique items back to recent.txt file 
    uniquerecentStreamWriter.Write(uniqueratingFiles)                       'Write the list of recently opened files back to the file Recent.txt
    uniquerecentStreamWriter.Close()                                             'Close the file writing
    Currently the actual file path and name are not being written to the file, instead System.Linq.Enumerable+<DistinctIterator>d__64`1[System.String] is being written to the file. Debugging during runtime every thing is correct until the line
    Code:
    Dim uniqueratingFiles = currentFileContents.Distinct()
    uniqueratingFiles equals what is being written to the file. Drilling down into uniqueratingFiles I actually find what is supposed to be written to the file.

    I am also fairly certain that their is an easier way to accomplish this but I don't know what it is.

    Any help would be appreciated.

    Thank you

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,673

    Re: Recent file list troubles

    Like most LINQ methods, Distinct doesn't actually give you the values but, rather, an unrealised query against the original list. You have to actually realise the query to get the values. One way to realise the query is to enumerate it with a For Each loop. In your case though, what you actually want is to call ToArray or, possibly, ToList. That will give you a String array or a List(Of String).

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,673

    Re: Recent file list troubles

    If I was doing this, I wouldn't care about the file contents and I've never seen an application that does. If you have two files in different locations then they should be considered different files, even if the content is the same. If you exclude one because it has the same content as another and then that content changes externally, you're excluding a file that is not the same.

    What I would do is use a StringCollection in My.Settings to persist the list. I'd load it into a List(Of String) for use like so:
    vb.net Code:
    1. Private Const MAX_RECENT_FILE_COUNT As Integer = 10
    2.  
    3. Private recentFileList As New List(Of String)
    4.  
    5. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.     'Load the persisted list into the working list.
    7.     recentFileList.AddRange(My.Settings.RecentFileList.Cast(Of String)())
    8. End Sub
    9.  
    10. Private Sub AddRecentFile(filePath As String)
    11.     'Take the current list, add the new item to the beginning, remove duplicates, trim to max length and then create a new list.
    12.     recentFileList = recentFileList.Prepend(filePath).Distinct().Take(MAX_RECENT_FILE_COUNT).ToList()
    13. End Sub
    14.  
    15. Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    16.     'Load the working list into the persisted list.
    17.     My.Settings.RecentFileList.Clear()
    18.     My.Settings.RecentFileList.AddRange(recentFileList.ToArray())
    19. End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent file list troubles

    Quote Originally Posted by jmcilhinney View Post
    Like most LINQ methods, Distinct doesn't actually give you the values but, rather, an unrealised query against the original list. You have to actually realise the query to get the values. One way to realise the query is to enumerate it with a For Each loop. In your case though, what you actually want is to call ToArray or, possibly, ToList. That will give you a String array or a List(Of String).
    I changed

    Code:
    Dim uniqueratingFiles = currentFileContents.Distinct()
    To

    Code:
    Dim uniqueratingFiles = currentFileContents.Distinct().ToArray
    Now System.String[] is being written to the file.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent file list troubles

    Quote Originally Posted by jmcilhinney View Post
    If I was doing this, I wouldn't care about the file contents and I've never seen an application that does. If you have two files in different locations then they should be considered different files, even if the content is the same. If you exclude one because it has the same content as another and then that content changes externally, you're excluding a file that is not the same.

    What I would do is use a StringCollection in My.Settings to persist the list. I'd load it into a List(Of String) for use like so:
    vb.net Code:
    1. Private Const MAX_RECENT_FILE_COUNT As Integer = 10
    2.  
    3. Private recentFileList As New List(Of String)
    4.  
    5. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.     'Load the persisted list into the working list.
    7.     recentFileList.AddRange(My.Settings.RecentFileList.Cast(Of String)())
    8. End Sub
    9.  
    10. Private Sub AddRecentFile(filePath As String)
    11.     'Take the current list, add the new item to the beginning, remove duplicates, trim to max length and then create a new list.
    12.     recentFileList = recentFileList.Prepend(filePath).Distinct().Take(MAX_RECENT_FILE_COUNT).ToList()
    13. End Sub
    14.  
    15. Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    16.     'Load the working list into the persisted list.
    17.     My.Settings.RecentFileList.Clear()
    18.     My.Settings.RecentFileList.AddRange(recentFileList.ToArray())
    19. End Sub
    I am not using two files, I am only using one.

    I am not familiar with My.Settings, where can I find more information about this.

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,673

    Re: Recent file list troubles

    And guess what: you still haven't shown us where you're writing anything to that file. You are obviously doing it wrong. It would appear that you are calling ToString, either explicitly or implicitly, on uniqueratingFiles and writing the result of that, which is wrong. My guess is that you have Option Strict Offand it's implicit.

    [/I]In the vast majority of cases, calling ToString on an object will simply return the name of its type, which is exactly what you are seeing. It's only very simple types that produce something else, e.g. numeric types. If you have a list and you want to write that to a file, the most sensible way would like be to call File.WriteAllLines. You pass the list and it will automatically write each item to its own line in the file, calling ToString internally on each item, rather than on the list itself. Given that File.WriteAllLines will accept an IEnumerable(Of T), you don't need the ToArray call.
    Last edited by jmcilhinney; Feb 11th, 2020 at 07:04 PM.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,673

    Re: Recent file list troubles

    Quote Originally Posted by Tesla1886 View Post
    I am not using two files, I am only using one.
    That makes no sense in this context.
    Quote Originally Posted by Tesla1886 View Post
    I am not familiar with My.Settings, where can I find more information about this.
    Anyone who thinks that they can write software should not need search engines explained to them.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    116

    Re: Recent file list troubles

    Quote Originally Posted by jmcilhinney View Post
    That makes no sense in this context.

    Anyone who thinks that they can write software should not need search engines explained to them.
    No need to get rude!

    Search engines can be great yes, but they can also be a detriment!

  9. #9
    Registered User
    Join Date
    Feb 2020
    Posts
    3

    Re: Recent file list troubles

    How to get Recent Folders displaying in Windows 10 permanently: 1) Start a Run dialog box by either right-clicking the Start button and choose Run, or by pressing the WinKey + R keys. 3) File Explorer will open and display the "Recent folders". ... 4) Right-click "Recent folders" and select "Pin to Quick Access".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width