VB.Net Get FTP list of files


Imports System.Net

Public Class FTP1

    Private host As String
    Private username As String
    Private password As String

    Sub New(hostValue As String, usernameValue As String, passwordValue As String)
        host = hostValue
        username = usernameValue
        password = passwordValue
    End Sub

    Function GetFileList() As List(Of String)

        Dim ftp As System.Net.FtpWebRequest = CType(FtpWebRequest.Create(host), System.Net.FtpWebRequest)
        ftp.Credentials = New System.Net.NetworkCredential(username, password)
        ftp.KeepAlive = False
        ftp.UseBinary = True
        ftp.Method = WebRequestMethods.Ftp.ListDirectory
        Dim streamReader As New IO.StreamReader(ftp.GetResponse.GetResponseStream)

        Dim result As New List(Of String)
        Dim line = streamReader.ReadLine
        While Not String.IsNullOrEmpty(line)
            line = streamReader.ReadLine
        End While
        Return result

    End Function

End Class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: