Page 1 of 3

Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 3:41 am
by GoodGuy17
Hello,
I am going to show you how to make your own file downloader.
First, let me explain what it does.
When you enter a real URL of a download, and choose where you want to save it, click download and it will show the stats of the download.
OK, start up VB.NET and add the following:
3 buttons - Names: brws, btn_download, btn_cancel
9 labels - Names: Label1, Label2, lblname, lbldownloading, lblsloc, lblsize, lblspd, lblstat, lblpercent
2 textboxes - Names: txtfilename, loc
1 progressbar - Name: ProgressBar1
1 FolderBrowserDialog - Name: FBD
1 SaveFileDialog - Name: SaveFileDialog1
1 BackgroundWorker - Name: BackgroundWorker1
You MUST put this above Public Class:
Code: Select all
Imports System.Net
Add this below Public Class:
Code: Select all
Dim whereToSave 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)
Now, arrange your controls on the form similarly to this and change the captions of some controls to this:
Image
Now, add this code to Download click event:
Code: Select all
        If Me.txtURL.Text <> "" AndAlso Me.txtfilename.Text.StartsWith("http://") Then
            Me.whereToSave = Me.loc.Text
            Me.SaveFileDialog1.FileName = ""
            Me.lblsloc.Text = "Save to : " & whereToSave
            Me.txtfilename.Enabled = False
            Me.btn_download.Enabled = False
            Me.btn_cancel.Enabled = True
            Me.loc.Enabled = False
            Me.brws.Enabled = False
            Me.BackgroundWorker1.RunWorkerAsync()
        Else
            MessageBox.Show("This url is not valid", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
Add this code to the Cancel click event:
Code: Select all
 Me.BackgroundWorker1.CancelAsync()
        loc.Enabled = True
        brws.Enabled = True
Add this code to the ... button click event:
Code: Select all
 Me.SaveFileDialog1.FileName = Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.lblname.Text = "Name : " & Me.txtfilename.Text.Split("/"c)(Me.txtfilename.Text.Split("/"c).Length - 1)
        Me.SaveFileDialog1.ShowDialog()
        Me.loc.Text = Me.SaveFileDialog1.FileName
Add this code to the backgroundworker dowork:
Code: Select all
   Me.btn_download.Enabled = False
        Dim theResponse As HttpWebResponse
        Dim theRequest As HttpWebRequest
        Try
            theRequest = WebRequest.Create(Me.txtfilename.Text)
            theResponse = theRequest.GetResponse
        Catch ex As Exception
            MessageBox.Show("An error occurred while downloading file. Possibe causes:" & ControlChars.CrLf & _
                            "1) File doesn't exist" & ControlChars.CrLf & _
                            "2) Remote server error", "Error", 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 / length) * 100
            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)
Right click the form and click View Code and add this under all of the End Subs:
Code: Select all
Public Sub DownloadComplete(ByVal cancelled As Boolean)
        Me.txtFileName.Enabled = True
        Me.btn_download.Enabled = True

        If cancelled Then
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Cancelled"
            MessageBox.Show("Download Cancelled!", "Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            loc.Enabled = True
            brws.Enabled = True
            Me.btn_cancel.Enabled = False
            Me.lblstat.Text = "Status : " & "Successfully downloaded"
            MessageBox.Show("Download Succeeded !", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If

        Me.ProgressBar1.Value = 0
    End Sub
    Public Sub ChangeTexts(ByVal length As Long, ByVal position As Integer, ByVal percent As Integer, ByVal speed As Double)

        Me.lblsize.Text = "Size : " & Math.Round((length / 1024), 2) & " KB"
        Me.lbldownloading.Text = "Downloading : " & Me.txtFileName.Text
        Me.lblstat.Text = "Status : " & Math.Round((position / 1024), 2) & " KB of " & Math.Round((length / 1024), 2) & "KB (" & Me.ProgressBar1.Value & "%)"
        Me.lblpercent.Text = Me.ProgressBar1.Value & "%"
        If speed = -1 Then
            Me.lblspd.Text = "Speed : " & "Calculating..."
        Else
            Me.lblspd.Text = "Speed : " & Math.Round((speed / 1024), 2) & " KB/s"
        End If

        Me.ProgressBar1.Value = percent
    End Sub
Test it out, and it should be working fine! This is my longest tutorial I've made wahooo;
Here is the source:
File Downloader.zip
Enjoy!
~GoodGuy17~ :D

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 7:19 am
by MasterCoding
give a example please For my Operating System

ps. you get credits in the credits box

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 8:34 am
by GoodGuy17
Do you mean the source code?

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 9:01 am
by MasterCoding
yes source code

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 11:00 am
by Nery
Pretty good, it'll be useful for me, since it is very advanced indeed.

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 6:01 pm
by GoodGuy17
MasterCoding, I will post source code. Hold on.

Re: Advanced Single-File Downloader

Posted: Sat Nov 14, 2009 6:13 pm
by GoodGuy17
OK, I posted the source and added a cancel button to the tutorial! :D

Re: Advanced Single-File Downloader

Posted: Wed Nov 18, 2009 11:36 pm
by RunarM
Could anyone help me make it like this:

You have a combo box [txtfilename] and like 10+ downloading options, and when you click on one of them then download then it downloades em.

Really need help o.O

Re: Advanced Single-File Downloader

Posted: Thu May 13, 2010 2:11 am
by lawrence12
whats the txtURL?
am i going to add three textboxes? or just 2
i named the first textbox
txtfilename
and the second textbox
loc
and then an error appeared it says
txtURL is not a member of My Form
:? can you please help me?

Re: Advanced Single-File Downloader

Posted: Thu May 13, 2010 2:21 am
by jeezy09
i saw this exact same thing on youtube lol cool program though