954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

List control to view list of files on FTP site

Hello,

I am attempting to put a list box in to my program, so that when the page loads, a list of files from my FTP site appear.

I also want it so that a file can be selected on the screen and downloaded.

Is there a way to do this?

If so, what sort of listbox should I be looking at? Because the only ones I have come across only connect to Access or SQL Server databases.

Please help...

Thanks,

Dan

dwinn
Junior Poster in Training
53 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 
stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

Thank you for your message stbuchok.

Ive now got it so the list of FTP files are appearing on the screen and I am now able to select a file from the list and download it.

The problem I have now is that when I load the page, instead of each file only appearing in the list once, each file appears multiple times.

Below is the code that I am using:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
                Dim ftp As FtpWebRequest = DirectCast(WebRequest.Create("ftp://xx.x.xx.xx/In/"), FtpWebRequest)
                
                ftp.Method = WebRequestMethods.Ftp.ListDirectory
                
                Dim ftpFiles As New ArrayList()
                
                ftp.Credentials = New NetworkCredential("user", "password")
                
                Dim Response As FtpWebResponse = ftp.GetResponse()
 
                Dim responseStream As Stream = Response.GetResponseStream()
 
                Dim reader = New StreamReader(responseStream)
                
                While Not (reader.EndOfStream)
                    
                    ftpFiles.Add(reader.ReadLine())
                    
                    'ListBox1.Items.Add(ftpFiles.ToString())
                    
                    For Each file In ftpFiles
                        
                        ListBox1.Items.Add(file)
                        
                    Next

                End While
                
                reader.Close()
 
                responseStream.Close()
 
                Response.Close()

            End Sub


I want it so that each file only appears once in the list.

All help and advice will be greatly appreciated.

Thanks,

Dan

dwinn
Junior Poster in Training
53 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Move the For Each loop after the While loop.

While Not (reader.EndOfStream)
 
                    ftpFiles.Add(reader.ReadLine())
 
                    'ListBox1.Items.Add(ftpFiles.ToString())
 
                End While

                For Each file In ftpFiles
 
                    ListBox1.Items.Add(file)
 
                Next
stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

Hi stbuchok,

Thank you very much!!!

It works perfectly.

Much appreciated.

Dan

dwinn
Junior Poster in Training
53 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

[Solved] This article will help you http://codereflex.net/how-to-list-files-from-ftp/

codereflex
Newbie Poster
5 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: