Here is the full code for the download form
Imports System.Net
Public Class downloader
Public whereToSave As String
Public dir As String
Delegate Sub ChangeTextsSafe(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Delegate Sub DownloadCompleteSafe(ByVal cancelled As Boolean)
Public Sub DownloadComplete(ByVal cancelled As Boolean)
Me.txtFileName.Enabled = True
Me.btnDownload.Enabled = True
Me.btnCancel.Enabled = False
If cancelled Then
Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_1")
Else
Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_2")
End If
Me.ProgressBar1.Value = 0
Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5") + " "
Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6") + " "
Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3") + " "
Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2") + " "
Me.Label4.Text = ""
End Sub
Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)
Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3") + " " & Math.Round((length / 1024), 2) & " KB"
Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5") + " " & Me.txtFileName.Text
Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4_3") + " " & Math.Round((position / 1024), 2) & " KB " + Main_Win.rm.GetString("Downloader_Label4_4") + " " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
If speed = -1 Then
Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2_2")
Else
Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2") + " " & Math.Round((speed / 1024), 2) & " KB/s"
End If
Me.ProgressBar1.Value = percent
If percent = 100 Then
Me.Close()
End If
End Sub
Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
If Me.txtFileName.Text <> "" Then
Me.SaveFileDialog1.InitialDirectory = dir
Me.SaveFileDialog1.FileName = whereToSave
If Me.SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.whereToSave = Me.SaveFileDialog1.FileName
Me.SaveFileDialog1.FileName = ""
Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6") + " " & Me.whereToSave
Me.txtFileName.Enabled = False
Me.btnDownload.Enabled = False
Me.btnCancel.Enabled = True
Me.BackgroundWorker1.RunWorkerAsync()
End If
Else
MessageBox.Show(Main_Win.rm.GetString("Downloader_msgbox1"), Main_Win.rm.GetString("Downloader_msgbox1_1"), MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
Try
theRequest = WebRequest.Create(Me.txtFileName.Text)
theResponse = theRequest.GetResponse
Catch ex As Exception
MessageBox.Show(Main_Win.rm.GetString("Downloader_msgbox2") & ControlChars.CrLf & _
Main_Win.rm.GetString("Downloader_msgbox2_2") & ControlChars.CrLf & _
Main_Win.rm.GetString("Downloader_msgbox2_3"), Main_Win.rm.GetString("Downloader_msgbox2_4"), MessageBoxButtons.OK, MessageBoxIcon.Error)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End Try
Dim length As Long = theResponse.ContentLength
Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
Me.Invoke(safedelegate, length, 0, 0, 0)
Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
Dim nRead As Integer
Dim speedtimer As New Stopwatch
Dim currentspeed As Double = -1
Dim readings As Integer = 0
Do
If BackgroundWorker1.CancellationPending Then
Exit Do
End If
speedtimer.Start()
Dim readBytes(4095) As Byte
Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
nRead += bytesread
Dim percent As Short = (nRead * 100) / length
Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
If bytesread = 0 Then Exit Do
writeStream.Write(readBytes, 0, bytesread)
speedtimer.Stop()
readings += 1
If readings >= 5 Then
currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
speedtimer.Reset()
readings = 0
End If
Loop
theResponse.GetResponseStream.Close()
writeStream.Close()
If Me.BackgroundWorker1.CancellationPending Then
IO.File.Delete(Me.whereToSave)
Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(cancelDelegate, True)
Exit Sub
End If
Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
Me.Invoke(completeDelegate, False)
End Sub
Private Sub Downloader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Label1.Text = Main_Win.rm.GetString("Downloader_Label1")
Me.Label2.Text = Main_Win.rm.GetString("Downloader_Label2")
Me.Label3.Text = Main_Win.rm.GetString("Downloader_Label3")
Me.Label4.Text = Main_Win.rm.GetString("Downloader_Label4")
Me.Label5.Text = Main_Win.rm.GetString("Downloader_Label5")
Me.Label6.Text = Main_Win.rm.GetString("Downloader_Label6")
Me.btnDownload.Text = Main_Win.rm.GetString("Downloader_btnDownload")
Me.btnCancel.Text = Main_Win.rm.GetString("Downloader_btnCancel")
Me.Label4.Text = ""
Me.btnDownload.ForeColor = Color.Black
Me.btnCancel.ForeColor = Color.Black
End Sub
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.BackgroundWorker1.CancelAsync()
End Sub
End Class
and thgis is the request handler in the main form:
Public Sub WebKitBrowser1_DownloadBegin(Sender As Object, e As WebKit.FileDownloadBeginEventArgs)
Dim time As DateTime = DateTime.Now
Dim format As String = "d/M/yyyy HH:mm"
Dim newItem As New ListViewItem(e.SuggestedFileName)
downloader.Show()
downloader.txtFileName.Text = e.Url.ToString
downloader.whereToSave = e.SuggestedFileName
downloader.dir = GetDownloadsPath()
newItem.SubItems.Add(e.Url.ToString)
newItem.SubItems.Add(time.ToString(format))
ListView1.Items.Add(newItem)
My.Settings.Downloads.Add(e.SuggestedFileName + "|" + e.Url.ToString + "|" + time.ToString(format))
My.Settings.Save()
End Sub
and this is the getdownloadpath function(if necessarily)
<DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function SHGetKnownFolderPath(ByRef id As Guid, flags As Integer, token As IntPtr, ByRef path As IntPtr) As Integer
End Function
Public Function GetDownloadsPath() As String
Dim path__1 As String = Nothing
If Environment.OSVersion.Version.Major >= 6 Then
Dim pathPtr As IntPtr
Dim hr As Integer = SHGetKnownFolderPath(FolderDownloads, 0, IntPtr.Zero, pathPtr)
If hr = 0 Then
path__1 = Marshal.PtrToStringUni(pathPtr)
Marshal.FreeCoTaskMem(pathPtr)
Return path__1
End If
End If
path__1 = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal))
path__1 = Path.Combine(path__1, "Downloads")
Return path__1
End Function