שתף באמצעות


How to read the files from ftp server and display at textbox in vb.net

Question

Wednesday, January 4, 2017 3:07 PM

Hi guys,

I want to display the text files context from my ftp server into my text box. I can only find the codes that display the text file context in the physical drive like "c:/". Can anyone teach me how can i do it from ftp server like "ftp://192.168.10.1/"?

Here is the code i found from website display the text file context in the physical drive.

Private Sub LoginForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New IO.StreamReader("D:\upload\announcement.txt")
        txtAnnouncement.Text = r.ReadToEnd
        r.Close()
    End Sub

Thank you for your help and guidance.

All replies (3)

Wednesday, January 4, 2017 7:54 PM ✅Answered

FtpWebRequest Class

How to: Download Files with FTP

Code below, from second link above, converted to VB.Net from C# using Telerik. You will also need to add an imports statement for System.Net and System.IO which if either is not referenced the Imports statement will error therefore if the Imporst statement is in error you will need to add appropriate references also. And the below first code is only valid for files which contain text which could be different file types such as .Txt or .Xml, etc. The second code is for saving downloaded files to disk I believe.

Also see this thread where the thread originator accesses multiple files on FTP server.

And the top link doesn't have VB.Net examples so you can use Telerik to convert the C# examples to VB.Net probably.

' Get the object used to communicate with the server.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create("ftp://www.contoso.com/test.htm"), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile

' This example assumes the FTP site uses anonymous logon.
request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")

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

Dim responseStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(responseStream)
TextBox1.Text = reader.ReadToEnd())

reader.Close()
response.Close()

The below code I had in a project but it's been awhile since I bothered with it.

Option Strict On

Imports System.Net
Imports System.IO

Public Class Form1

    Dim FilesList As New List(Of String)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox1.Clear()
        FilesList = GetFtpFileList("ftp://ftp.swfwmd.state.fl.us/pub/gisdata/stressed_lakes/", "anonymous", "nobody@nowhere.com")
        If FilesList.Count > 0 Then
            For i = 0 To FilesList.Count - 1
                RichTextBox1.AppendText(FilesList(i) & vbCrLf)
            Next
            If TextBox1.Text <> "" Then
                Using FBD As New FolderBrowserDialog
                    With FBD
                        .SelectedPath = "C:\Users\" & Environment.UserName & "\Desktop"
                        .Description = "Folder to download files to"
                        .ShowNewFolderButton = True
                    End With
                    If FBD.ShowDialog = Windows.Forms.DialogResult.OK Then
                        DownloadFiles(FilesList, TextBox1.Text, "anonymous", "nobody@nowhere.com", "ftp://ftp.swfwmd.state.fl.us/pub/gisdata/stressed_lakes/", FBD.SelectedPath)
                        'MessageBox.Show(FBD.SelectedPath)
                    End If
                End Using
            End If
        End If
    End Sub

    Private Function GetFtpFileList(ByVal Url As String, ByVal userName As String, ByVal password As String) As List(Of String)

        Dim request = DirectCast(WebRequest.Create(Url), FtpWebRequest)

        request.Method = WebRequestMethods.Ftp.ListDirectory

        request.Credentials = New NetworkCredential(userName, password)

        Using reader As New StreamReader(request.GetResponse().GetResponseStream())
            Dim line = reader.ReadLine()
            Dim lines As New List(Of String)

            Do Until line Is Nothing
                lines.Add(line)
                line = reader.ReadLine()
            Loop

            Return lines
        End Using
    End Function

    Private Sub DownloadFiles(ByRef Files As List(Of String), ByRef Patterns As String, ByRef UserName As String, ByRef Password As String, ByRef Url As String, ByRef PathToWriteFilesTo As String)
        Dim Pattern() As String = Patterns.Split("|"c)

        For i = 0 To Files.Count - 1
            For Each Item In Pattern
                If Files(i).ToUpper.Contains(Item.ToUpper) Then
                    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(Url & Files(i)), FtpWebRequest)
                    request.Method = WebRequestMethods.Ftp.DownloadFile

                    ' This example assumes the FTP site uses anonymous logon.
                    request.Credentials = New NetworkCredential("anonymous", "janeDoe@contoso.com")

                    Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)


                        'My.Computer.FileSystem.WriteAllBytes(" ", response.GetResponseStream)
                        Using responseStream As Stream = response.GetResponseStream()
                            Using MS As New IO.MemoryStream
                                responseStream.CopyTo(MS)
                                My.Computer.FileSystem.WriteAllBytes(PathToWriteFilesTo & "\" & Files(i), MS.ToArray, False)
                            End Using
                        End Using
                    End Using
                End If
            Next
        Next
    End Sub

End Class

La vida loca


Wednesday, January 4, 2017 8:17 PM

It worked. Thanks for your help~


Wednesday, January 4, 2017 9:00 PM

It worked. Thanks for your help~

La vida loca