Me.fs=NewFileStream(SaveFile,FileMode.CreateNew)'Create a new FileStream that writes to the desired download path
Me.downloadUrl=downloadUrl
Me.savePath=SaveFile
EndSub
Dim_speedSamplesAsList(OfDouble)
PublicSubStartTask()
IfDownloadingThenReturn
_Downloading=True
Call__downloadTask()
_Downloading=False
EndSub
PrivateSub__downloadTask()
DimreqAsWebRequest=WebRequest.Create(downloadUrl)'Make a request for the url of the file to be downloaded
DimrespAsWebResponse=req.GetResponse'Ask for the response
Dimbuffer(8192)AsByte'Make a buffer
DimdownloadedsizeAsLong=0
DimdownloadedTimeAsLong=0
DimdlSpeedAsLong=0
_TotalSize=req.ContentLength
_speedSamples=NewList(OfDouble)
_CurrentSize=0
DoWhile_CurrentSize<_TotalSize
DimreadAsInteger=resp.GetResponseStream.Read(buffer,0,8192)'Read the buffer from the response the WebRequest gave you
fs.Write(buffer,0,read)'Write to filestream that you declared at the beginning of the DoWork sub
_CurrentSize+=read
downloadedsize+=read
downloadedTime+=1'Add 1 millisecond for every cycle the While field makes
IfdownloadedTime=1000Then'Then, if downloadedTime reaches 1000 then it will call this part
dlSpeed=(downloadedsize/TimeSpan.FromMilliseconds(downloadedTime).TotalSeconds)'Calculate the download speed by dividing the downloadedSize by the total formatted seconds of the downloadedTime
downloadedTime=0'Reset downloadedTime and downloadedSize