Skip to main content

VB.NET
Downloading a File to Place Inside a TextBox

Form1.vb

' AZUL CODING ---------------------------------------
' VB.NET - Downloading a File to Place Inside a TextBox
' https://youtu.be/XklF9yNHCmw


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    DownloadFile(True)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    DownloadFile(False)

End Sub

Private Sub DownloadFile(delete As Boolean)
    Try
        If TextBox1.Text.EndsWith(".txt") Then
            Dim filename As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\Temp\temp.txt"

            My.Computer.Network.DownloadFile(TextBox1.Text, filename, "", "", True, 10000, False)
            TextBox2.Text = IO.File.ReadAllText(filename)

            If delete Then My.Computer.FileSystem.DeleteFile(My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\Temp\temp.txt",
                                                                FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)    
                        
        Else
            MsgBox("Web address must end with .txt")

        End If

    Catch ex As Exception
        MsgBox("Download error")

    End Try

End Sub

Enjoying this tutorial?