How to make a File Downloader

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
3 posts Page 1 of 1
Contributors
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

How to make a File Downloader
Usman55
Hello Everyone,

I think you have seen my new applications that I posted today. So one of them was a file downloader. So today I'm going to show you how to make a simple file downloader. Did I say simple? Oh yes I did! It means that the application will only download a file using a few controls. But if you would like to know how to add some extra features that my file downloader contains, PM me or post a comment about it. I'll be glad to tell.

---------------------------------------------------------------------------

First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox File Downloader and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.


First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to File Downloader. Change it's StartPosition property to CenterScreen. Change it's FormBorderStyle property to FixedSingle. Change it's MaximizeBox property to False.


Now add the following things to the form:

(1)_ 4 Labels. Change Label1's Text property to "Download URL:", Label2's to "Save URL:", Label3's to "Current Progress:" and Label4's to "0%" (Place it in the middle/center of the ProgressBar).

(2)_ 2 TextBoxes. Place TextBox1 in front of Label1 and TextBox2 in front of Label2.

(3)_ 4 Buttons. Change Button1's Text property to "Browse..." (Place it next to TextBox2), Button2's to "Download", Button3's to "Cancel" and Button4's to "Open File".

(4)_ 1 ProgressBar.

(5)_ 1 BackGroundWorker.


Now is the time to code Form1 and it's components. So right-click on the form in the Solution Explorer and click View Code.

Type the following code before the Public Class Form1:
Code: Select all
Imports Microsoft.Win32
Type the following code after the Public Class Form1:
Code: Select all
Dim URISource As Uri
    Dim Downloading As New Net.WebClient
    Dim TimerID As IntPtr = 0

    Sub SetupEventHandlers()
        Try
            AddHandler Downloading.DownloadProgressChanged, AddressOf GetDownloadProgress
            AddHandler Downloading.DownloadFileCompleted, AddressOf DownloadHasEnded
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Sub DoDownload()
        Try
            Downloading.DownloadFileAsync(URISource, TextBox2.Text)
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Sub DownloadHasEnded(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
        Try
            If e.Cancelled Then
                MsgBox("Download has been cancelled!", MsgBoxStyle.Information, "Download Cancelled!")
            Else
                MsgBox("Download has been completed!", MsgBoxStyle.Information, "Download Completed!")
            End If
            If TimerID.ToInt32 > 0 Then
                SystemEvents.KillTimer(TimerID)
                TimerID = Nothing
            End If
        Catch exc As Exception
            MessageBox.Show(e.Error.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Sub GetDownloadProgress(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
        Try
            ProgressBar1.Value = e.ProgressPercentage
            Label4.Text = e.ProgressPercentage.ToString & "%"
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
Double-click the Form using the Designer window and enter the following code in the Form1_Load event:
Code: Select all
Try
            SetupEventHandlers()
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
And enter this code in the Form1_FormClosing event using the same process:
Code: Select all
If ProgressBar1.Value = 0 OrElse 100 Then

        Else
            Dim dlgrst As DialogResult = MessageBox.Show("Download has not finished yet. Are you sure you want to exit?", "Download Not Finished!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
            If dlgrst = DialogResult.Yes Then

            ElseIf dlgrst = DialogResult.No Then
                e.Cancel = True
            End If
        End If
Add the following code to Button1 by double-clicking it:
Code: Select all
Dim saveDLG As New SaveFileDialog
        Dim ext As String = "|Exe(*.exe)|*.exe"
        saveDLG.Filter = "All Files (*.*)|*.*" & ext
        saveDLG.Title = "Select the path and filename to save the download." & vbNewLine & vbNewLine & "Be sure to specify the extension."
        If saveDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox2.Text = saveDLG.FileName
        End If
Add the following code to Button2 by double-clicking it:
Code: Select all
Try
            URISource = New Uri(TextBox1.Text)
            TimerID = SystemEvents.CreateTimer((1000))
            DoDownload()
            ProgressBar1.Value = 0
            MsgBox("Download has been started!", MsgBoxStyle.Information, "Download Started!")
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
Add the following code to Button3 by double-clicking it:
Code: Select all
Try
            If TimerID.ToInt32 > 0 Then
                SystemEvents.KillTimer(TimerID)
                TimerID = Nothing
            End If
            Downloading.CancelAsync()
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
Add the following code to Button4 by double-clicking it:
Code: Select all
Try
            If My.Computer.FileSystem.FileExists(TextBox2.Text) Then
                Process.Start(TextBox2.Text)
            Else
                MessageBox.Show("The file does not exists or has been deleted." & vbNewLine & vbNewLine & "Make sure the file was downloaded and saved successfully.", "Unable To Open File.", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch exc As Exception
            MessageBox.Show(exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
That's all folks! It was just a basic one but as I said before, if you want any of the features from my ObsprO File Downloader just ask me.


---------------------------------------------------------------------------

Great Job! You have just completed making a File Downloader. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.

And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.

Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a File Downloader project myself so you can download the source file in the attachments.

Thank you.

Image
You do not have the required permissions to view the files attached to this post.
Image
User avatar
Bogoh67
VIP - Site Partner
VIP - Site Partner
Posts: 656
Joined: Sun Apr 18, 2010 8:20 pm

Re: How to make a File Downloader
Bogoh67
instead of putting a label in the center just type
Code: Select all
Private Sub change_status(ByVal e As System.Net.DownloadProgressChangedEventArgs)
        ProgressBar1.Refresh()
        Dim text As System.Drawing.Font = My.Settings.font
        ProgressBar1.CreateGraphics.DrawString(e.ProgressPercentage & "%", Font, Brushes.Black, 10, 10)
End Sub
then
Code: Select all
Sub GetDownloadProgress(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
       change_status()
    End Sub
p.s. dont get mad at me if this doesnt work
,code wasnt tested
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a File Downloader
Usman55
That would be alot of code and would make the program a bit slow so its not a good idea.
Image
3 posts Page 1 of 1
Return to “Tutorials”