【VISUAL VB.NET】Download file via HTTP

 Imports System

Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Forms ' make sure that using System.Diagnostics; is included Imports System.Diagnostics ' make sure that using System.Security.Principal; is included Imports System.Security.Principal ' make sure that using System.Net; is included Imports System.Net Public Class Form1 Public Sub New() MyBase.New InitializeComponent End Sub Private webClient As New WebClient() Private Sub webClient_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Dim bytesIn As Double = Double.Parse(e.BytesReceived.ToString()) Dim totalBytes As Double = Double.Parse(e.TotalBytesToReceive.ToString()) Dim percentage As Double = bytesIn / totalBytes * 100 progressBar1.Value = Integer.Parse(Math.Truncate(percentage).ToString()) End Sub Private Sub webclient_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) MessageBox.Show("Saved as C:\MYRAR.EXE", "Httpdownload") End Sub #Region "basic function for app" Private Sub lblLink_Click_1(sender As Object, e As EventArgs) Handles lblLink.Click Process.Start("www.vclexamples.com") End Sub Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean If (keyData = Keys.Escape) Then Me.Close() Return True End If Return MyBase.ProcessCmdKey(msg, keyData) End Function #End Region Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click AddHandler WebClient.DownloadProgressChanged, New DownloadProgressChangedEventHandler(AddressOf webClient_DownloadProgressChanged) AddHandler WebClient.DownloadFileCompleted, New AsyncCompletedEventHandler(AddressOf webclient_DownloadFileCompleted) ' start download WebClient.DownloadFileAsync(New Uri(textBox1.Text), "C:\\MYRAR.EXE") End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class

No comments:

Contact Form

Name

Email *

Message *