Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDownload a file from the web and save it with an arbitrary local file name in Visual Basic .NET
DescriptionThis example shows how to download a file from the web and save it with an arbitrary local file name in Visual Basic .NET.
Keywordsdownload, download file, web, internet, WebClient, ftp, file transfer, Visual Basic, VB.NET
CategoriesInternet, Files and Directories
 
This is actually pretty easy using a WebClient object.

First, add an "Imports System.Net" statement to the top of the file.

The folowing code shows how the program responds when you click the Download button.

 
Private Sub btnDownload_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnDownload.Click
    Me.Cursor = Cursors.WaitCursor
    Application.DoEvents()

    Try
        ' Make a WebClient.
        Dim web_client As WebClient = New WebClient

        ' Download the file.
        web_client.DownloadFile(txtRemoteFile.Text, _
            txtLocalFile.Text)

        MessageBox.Show("Done")
    Catch ex As Exception
        MessageBox.Show(ex.Message, "Download Error", _
            MessageBoxButtons.OK, _
                MessageBoxIcon.Exclamation)
    End Try

    Me.Cursor = Cursors.Default
End Sub
 
The code simply creates a WebClient object and invokes its DownloadFile method passing it the remote file's URL and the destination file's name. That's all there is to it!
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated