none
Sorting listbox items descending RRS feed

  • Question

  • So I found a bunch of tutorials on how to sort listbox items but I am not sure if I'm not understanding the logic behind it. Here is what im doing

    I am basically querying a remote machine for local profiles on the computer C:\Users folders. Which work, it will return me the profiles and I add the last write date and then I add those items to the listbox. So for this it works:

     Try
                    'Get profile list
                    Dim ProfilePath As String = "\\" & tbDeviceName.Text & "\C$\Users"
    
                    Dim files() As String = Directory.GetDirectories(ProfilePath)
                    Dim file As String
    
                    For Each file In files
                        Dim LastWriteTime As String = Directory.GetLastWriteTime(file)
                        lstProfiles.Items.Add(LastWriteTime & " - " & file.Replace(ProfilePath, "").Replace("\", "") & vbCrLf)
                    Next
    
                    ' Remove unwated crap 
                    For i As Integer = lstProfiles.Items.Count - 1 To -1 + 1 Step -1
                        If lstProfiles.Items(i).Contains("Default") Or lstProfiles.Items(i).Contains("Default User") Or lstProfiles.Items(i).Contains("Public") Or lstProfiles.Items(i).Contains("All Users") Or lstProfiles.Items(i).Contains(".NET v4.5 Classic") Or lstProfiles.Items(i).Contains(".NET v4.5") Or lstProfiles.Items(i).Contains(".NET v2.0") Or lstProfiles.Items(i).Contains(".NET v2.0 Classic") Or lstProfiles.Items(i).Contains("Classic .NET AppPool") Then
                            lstProfiles.Items.RemoveAt(i)
                        End If
                    Next
                Catch ex As Exception
                    MsgBox("Issue querying user profiles: " & ex.Message)
                    Exit Sub
                End Try

    So after this point I would like to order my listbox items by last write Dates DESC 

    At this point I am just wondering if someone has a piece of code I can use or point me in the right direction. I have spend too much time I think on this little thing :/

    Cheers

    Sunday, November 5, 2017 3:04 PM

Answers

  •  It appears that you are just getting the Directories,  not actually the files.  You could use the OrderByDescending Extension to order the directories by their LastWriteTime as shown below.   I would use the DirectoryInfo Class instead of the Directory class to get the directories or files,  it makes things easier in this case.  It returns an array of DirectoryInfo which has the Name property which is just the directory name.  No need in manipulating the full path to get it as you are doing now.  The DirectoryInfo class also has the LastWriteTime property included in the info too.

     Perhaps you can just do it all in one swipe instead of loading the directories,  then sorting them,  then adding them to the ListBox...

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ProfilePath As String = "C:\TestFolder"
    
            Dim Folders() As DirectoryInfo = New DirectoryInfo(ProfilePath).GetDirectories.OrderByDescending(Function(x) x.LastWriteTime).ToArray
    
            For Each di As DirectoryInfo In Folders
                ListBox1.Items.Add(di.Name & "  " & di.LastWriteTime.ToString)
            Next
        End Sub
    
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by breaup Sunday, November 5, 2017 4:49 PM
    Sunday, November 5, 2017 3:53 PM

All replies

  •  It appears that you are just getting the Directories,  not actually the files.  You could use the OrderByDescending Extension to order the directories by their LastWriteTime as shown below.   I would use the DirectoryInfo Class instead of the Directory class to get the directories or files,  it makes things easier in this case.  It returns an array of DirectoryInfo which has the Name property which is just the directory name.  No need in manipulating the full path to get it as you are doing now.  The DirectoryInfo class also has the LastWriteTime property included in the info too.

     Perhaps you can just do it all in one swipe instead of loading the directories,  then sorting them,  then adding them to the ListBox...

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim ProfilePath As String = "C:\TestFolder"
    
            Dim Folders() As DirectoryInfo = New DirectoryInfo(ProfilePath).GetDirectories.OrderByDescending(Function(x) x.LastWriteTime).ToArray
    
            For Each di As DirectoryInfo In Folders
                ListBox1.Items.Add(di.Name & "  " & di.LastWriteTime.ToString)
            Next
        End Sub
    
    End Class
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by breaup Sunday, November 5, 2017 4:49 PM
    Sunday, November 5, 2017 3:53 PM
  • That did the trick you just save me a headache I appreciate it. 

    Cheers

    Sunday, November 5, 2017 4:49 PM