0

Currently working on a FTP client in vb.net using FTPwebrequest. I have successfully made it work, it's uploading and retrieving all files and displaying it in a Listview. My concern right now is to Sort/Arranged the items in remote directory showed in listview to display the newly added items first and so on.

I have already tried researching online but to no avail. Still looking rightnow.

here is the code for displaying the contents of the directory

Private Sub SampleProcedure1()
        Try
            'Create an FTP web request
            Dim ftpwebrequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://" & settings.txtserveraddress.Text & "/" & settings.foldername.Text & "/"), FtpWebRequest)
            'Set properties
            With ftpwebrequest
                'ftp server username and password
                .Credentials = New NetworkCredential(settings.txtserverusername.Text, settings.txtserverpassword.Text)
                'set the method to download
                .Method = WebRequestMethods.Ftp.ListDirectory
                'upload timeout to 100 seconds
                .Timeout = "100000"
            End With

            Dim ftpwebres As FtpWebResponse = CType(ftpwebrequest.GetResponse(), FtpWebResponse)
            Dim ftpstreamreader As StreamReader = New StreamReader(ftpwebres.GetResponseStream())
            'clear list of files
            ftpclient.lstfiles.Items.Clear()
            'start loading files from an FTP server into list
            While (ftpstreamreader.Peek() > -1)
                ftpclient.lstfiles.Items.Add(ftpstreamreader.ReadLine())
            End While
            ftpstreamreader.Close()
            ftpwebres.Close()
        Catch ex As Exception
            ftpclient.Cursor = Cursors.Default
            ftpclient.NotifyIcon1.ShowBalloonTip(1000, "Test FTP Client", ex.Message, ToolTipIcon.Info)

        End Try
    End Sub

The Listview should display/sort first base on the newest added/uploaded item. right not it sorts backwards and sorts alphabetical.

  • Use the `WebRequestMethods.Ftp.ListDirectoryDetails`. It contains the informations you need to `.OrderBy()` date `.ThenBy()` name (or it should :). – Jimi May 16 '19 at 10:19
  • Start here: [C# - Download files from FTP which have higher last-modified date](https://stackoverflow.com/q/38073302/850848) – Martin Prikryl May 18 '19 at 10:02
  • thank you for your suggestions. what i have applied so far the sorting of listbox itself. 'ftpclient.lstfiles.Sorting = SortOrder.Descending' also im going to try and see if there is similar function like this with ftpwebrequest 'For Each File As String In myDirectory.GetFiles.OrderByDescending(Function(x) x.LastWriteTime).Select(Function(x) x.CreationTime.Date = DateTime.Today.Date.ToString) listAllfiles.Items.Add(File) Next' – dexter delleva Jun 10 '19 at 06:01

0 Answers0