[OpenSource] Proper Downloader

Posts 1–15 of 17 · Page 1 of 2
[OpenSource] Proper Downloader
Note: Downloaders are restricted on MPGH. Therefore, don't use them in the programs you release!

I decided to release a small opensource project for a proper downloader.

The whole project is included.

[PHP]'This code is fully self-written.
'(c) Blubb1337
'Enjoy

Imports System.IO
Imports System.Text
Imports System.Net

'Since a lot of people seem to need a proper downloader, I decided 2 release a PROPER updater
'Most updater use my.network.downloadfile, however this does not show progress and it generally sucks!
'This updater uses a webclient. Using a webclient, you can display the current progress, the received bytes
'and the total bytes to receive.

'You also have a download completed event.

'How to use this?
'This is really easy, the only thing you need to change -> 4 Variables
'links
'Private Const _version As String = "http:/version.txt"
'Private Const _FileToDL As String = "http:/Recorder.exe"
'Private Const _log As String = "http://changelog.txt"
'name
'Private Const _Name As String = "Recorder"

'I think this is pretty self-explanatory. You sure don't need to have a log, you can just take it out.

'Furthermore, this updater uses multithread to check the newest version/load the checklog.
'Usually the program executes one sub/function/action after another. With multithreads you can run
'different subs/functions or whatever at the same time!
'You may also change the prioritys of the multithreads

'If you want to unpack a file after it has been downloaded, you can go for the sharpziplibrary:
'http://www.mpgh.net/forum/33-visual-basics/126996-tutorial-unzipping-vb-net.html

'Sub ExtractZip(ByVal FileToUnzip As String, ByVal WhereToSave As String, Optional ByVal Password As String = "")
'Dim fz As New FastZip
' fz.Password = Password
' fz.ExtractZip(FileToUnzip, WhereToSave, "")
' End Sub

Public Class Form1

#Region "Declarations"
'new webclient to for downloading
WithEvents webC As New Net.WebClient

Private apppath As String = Application.StartupPath & "\"
Private filename As String

'avoid the same filename twice
Private i As Integer = 1

'received/total bytes
Private total, current As Integer

'links
Private Const _version As String = "http://version.txt"
Private Const _FileToDL As String = "http://Recorder.exe"
Private Const _log As String = "http://changelog.txt"

'name
Private Const _Name As String = "Recorder"

'get a pages source code
Private Function loadPage(ByVal page As String)
Dim req As WebRequest = WebRequest.Create(page)
Dim resp As WebResponse = req.GetResponse()

Dim s As Stream = resp.GetResponseStream()
Dim sr As StreamReader = New StreamReader(s, Encoding.ASCII)
Dim doc As String = sr.ReadToEnd()
Return doc
End Function

#End Region

#Region "Download/Progress"

Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDownload.Click
If cmdDownload.Text = "Download" Then
'start backgroundworker
bgwDownload.RunWorkerAsync()
Else
'start the program
Process.Start(filename)
'end this application
Application.Exit()
End If
End Sub

Private Sub webC_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles webC.DownloadFileCompleted
' MsgBox("Download complete." & vbNewLine & vbNewLine _
' & "Saved at: " & filename)

cmdDownload.Text = "Start " & _Name
cmdDownload.Enabled = True
End Sub

Private Sub webC_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles webC.DownloadProgressChanged
current = e.BytesReceived 'bytes it dled already
total = e.TotalBytesToReceive 'total bytes it has to dl

If total <> current Then 'if they are not the same
cmdDownload.Enabled = False
End If

pbProgress.Value = e.ProgressPercentage 'show the procent percentage
'pbProgress.Text = CInt(current / 1024) & "/" & CInt(total / 1024) & " KB received | " & e.ProgressPercentage & " %"
lblprogress.Text = CInt(current / 1024) & "/" & CInt(total / 1024) & " KB received | " & e.ProgressPercentage & " %"
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwDownload.DoWork
'start downloading the file
webC.DownloadFileAsync(New Uri(_FileToDL), filename)
End Sub

#End Region

#Region "Startup"

Private Sub UpdateApp_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'to avoid threadovertaking actions -> program would just end
Control.CheckForIllegalCrossThreadCalls = False

'multithread so multiple things can be done at the same time -> load changelog
Dim cl As New Threading.Thread(AddressOf changelog)
cl.Priority = Threading.ThreadPriority.Highest
cl.Start()

'show the current version of the application
lblCurV.Text &= Application.ProductVersion

'to not replace the old version
Do While IO.File.Exists(apppath & _Name & "_Updated - " & i & ".exe")
i += 1
Loop

filename = apppath & _Name & "_Updated - " & i & ".exe"
End Sub

Private Sub changelog()
txtChangeLog.Text = loadPage(_log)
End Sub

Private Sub newversion()
lblNewV.Text &= loadPage(_version)
End Sub

Private Sub UpdateApp_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Dim checkv As New Threading.Thread(AddressOf newversion)
checkv.Priority = Threading.ThreadPriority.Highest
checkv.Start()
End Sub

#End Region
End Class
[/PHP]

Virus Scans:

Updater for MPGH.rar - Jotti&#039;s malware scan

VirusTotal - Free Online Virus, Malware and URL Scanner

Have fun copy & pasters!
Updater for MPGH.rarattachment deleted · 126 downloads before removal
gj
thanx for sharing it with us
Thanks I may use this in the future
Looks good, keep it up
its so big!!! ill rather use system.network.downloadfile...
Good Job. Keep it up..!
Quote Originally Posted by mnpeepno2 View Post
its so big!!! ill rather use system.network.downloadfile...
That's the point I am releasing it. Finally a proper downloader with progress ffs.
Looks good , Thanks
Quote Originally Posted by Icn2 View Post
Looks good , Thanks
do NOT say it
DO IT!
Added to tuts

Great job Kevin.
Thanks!

But I have a question,
If I have an update of the app, what should I change, the version.txt?

Quote Originally Posted by .Celtics View Post
Thanks!

But I have a question,
If I have an update of the app, what should I change, the version.txt?

Yes, you need to change the version.txt.
So, you can check the version using:

Code:
If Application.ProductVersion < CheckedVersion (version.txt) Then
Msgbox ("Update available !!")
End If
Simple
About the " Private Const _FileToDL As String = "http://Recorder.exe""

Im confused there , ill give an example

For Example
I released something, so at the 1st release, what will I put there? Since I dont have the link for the updated one?
Quote Originally Posted by .Celtics View Post
About the " Private Const _FileToDL As String = "http://Recorder.exe""

Im confused there , ill give an example

For Example
I released something, so at the 1st release, what will I put there? Since I dont have the link for the updated one?
Well you could get a FTP and have a static new version.exe link and then simply delete the old one and re-upload a newer version so it has the same link. OR you could make the program read a .txt document, similar how you check the version via the internet but get the link from there.

i.e (using Blubb's loadpage function)

[php]
Private Function loadPage(ByVal page As String)
Dim req As WebRequest = WebRequest.Create(page)
Dim resp As WebResponse = req.GetResponse()

Dim s As Stream = resp.GetResponseStream()
Dim sr As StreamReader = New StreamReader(s, Encoding.ASCII)
Dim doc As String = sr.ReadToEnd()
Return doc
End Function
[/php]

Then just do

[php]
Dim LinkToDownloadFrom As String = LoadPage("http://www.yourftp.com/getlatestversionlink.txt")
[/php]

And that will tell your program where to download the latest version.
Thanks! I get it naw

Very last question,

How do you make the progress bar have a text? Can find it in the properties

Sorry for being a pain-in-the-ass
Posts 1–15 of 17 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?