Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list directory contents of an FTP connection

Tags:

vb.net

ftp

I can't find a tutorial on this. In VB.NET I want to do a command such as:

Dim array1() as string = ListFilesInFolder("www.example.com/images")

I know this is probably not going to be that simple, but can anyone point me to a tutorial or anything?

like image 208
Dave Fes Avatar asked Dec 26 '22 12:12

Dave Fes


2 Answers

The following method will work for framework 3.5 and higher, I know this question is 3 years old but I ran into a situation where I need to list FTP directories in a framework 3.5 project So I wrote following code by referring How to: List Directory Contents with FTP.

Imports System.Net

Dim Dirlist As New List(Of String) 'I prefer List() instead of an array
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.example.com/images"), FtpWebRequest)

request.Method = WebRequestMethods.Ftp.ListDirectory
request.Credentials = New NetworkCredential("USER_NAME", "PASSWORD")

Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Dim responseStream As Stream = response.GetResponseStream

    Using reader As New StreamReader(responseStream)
          Do While reader.Peek <> -1
             Dirlist.Add(reader.ReadLine)
          Loop
    End Using
response.Close()
like image 34
Vivek S. Avatar answered Jan 12 '23 00:01

Vivek S.


use this free library http://netftp.codeplex.com/

Imports System.Net
Imports System.Net.FtpClient

Sub Main
    using ftp = new FtpClient()

        ftp.Host = "www.example.com"
        ftp.Credentials = new NetworkCredential("yourFTPUser", "yourFTPPassword")
        ftp.SetWorkingDirectory("/images")
        for each item in ftp.GetListing(ftp.GetWorkingDirectory())

            select case item.Type
                case FtpFileSystemObjectType.Directory:
                    Console.WriteLine("Folder:" + item.FullName)
                case FtpFileSystemObjectType.File:
                    Console.WriteLine("File:" + item.FullName)
            End Select
        Next
    End Using    
End Sub

of course I'm assuming that www.example.com is a FTP server.

AN IMPORTANT NOTE: The library requires the complete Framework 4.0. You should go to the Build Page of your Project Properties, click on the Advanced Options and select Framework 4.0 instead of Framework 4.0 Client Profile

like image 198
Steve Avatar answered Jan 11 '23 22:01

Steve