|
|
Title | Download a file from the web and save it with an arbitrary local file name in Visual Basic .NET |
Description | This example shows how to download a file from the web and save it with an arbitrary local file name in Visual Basic .NET. |
Keywords | download, download file, web, internet, WebClient, ftp, file transfer, Visual Basic, VB.NET |
Categories | Internet, 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!
|
|
|
|
|
|