• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a File Downloader in Visual Basic

lawa543

Cloud Automation Wizard
L Rep
0
0
0
Rep
0
L Vouches
0
0
0
Vouches
0
Posts
97
Likes
139
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Introduction:
Hello and welcome to a tutorial on how to make a single file downloader in Visual Basic. A word of advise; once you understand how this program works I would recommend adding the function to a new thread so the UI doesn't freeze while it is downloading.

Steps of Creation:
Step 1:

First we want to get a direct download link to a file, you can get this by downloading a file then going to your download manager (in chrome, firefox or any other browser/application) and copying the download link that is shown, I will be using a direct download link from the official website to download the English Windows 64Bit WinRar Application (link: http://www.rarlab.com/rar/winrar-x64-500.exe)

Step 2:
Now make a form with a textbox (direct download link) and a button to begin the process. I have kept the default names of textbox1 and button1 as we can easily tell the difference.

Step 3:
Next double click on button1 to get to the clicked event code, and add this:

  1. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  2. Dim

    url As

    String

    = checkURL()
  3. Try
  4. Dim

    fo As

    New

    SaveFileDialog
  5. fo.Filter = "Executable File|.exe|All Files|*"
  6. fo.FilterIndex = 1
  7. fo.RestoreDirectory = True
  8. fo.ShowDialog()
  9. Dim

    path As

    String

    = fo.FileName
  10. My.Computer.Network.DownloadFile(url, path)
  11. MsgBox("Finished!"

    )
  12. Catch ex As

    Exception
  13. MsgBox("Failed"

    )
  14. End

    Try
  15. End

    Sub

First we are getting the returned string from a function which we have not yet created called "checkURL" which is going to check the download link is in the correct format. Once it has the URL it is going to ask for a save location path and name and download the file to that directory. If it goes successfully it will output "Finished!", otherwise it will output "Failed".

Step 4:
Finally we want to make our checkURL function. As mentioned above, it will check the direct download link is in the correct format.

  1. Private

    Function

    checkURL()
  2. Dim

    s As

    String

    = TextBox1.Text.ToLower()
  3. If

    (s.StartsWith("http://www."

    ) Or

    s.StartsWith("https://www."

    )) Then
  4. Return s
  5. ElseIf

    (s.StartsWith("http://"

    )) Then
  6. If

    (s.Contains("www."

    )) Then
  7. s = "http://www."

    & s.Substring(1, s.Count - 1)
  8. TextBox1.Text = s
  9. Return s
  10. Else
  11. s = "http://www."

    & s.Substring(7, s.Count - 7)
  12. TextBox1.Text = s
  13. Return s
  14. End

    If
  15. ElseIf

    (s.StartsWith("https://"

    )) Then
  16. If

    (s.Contains("www."

    )) Then
  17. s = "https://"

    & s.Substring(12, s.Count() - 12)
  18. TextBox1.Text = s
  19. Return s
  20. Else
  21. s = "https://www."

    & s.Substring(8, s.Count - 8)
  22. TextBox1.Text = s
  23. Return s
  24. End

    If
  25. Else
  26. If

    (s.StartsWith("www."

    )) Then
  27. s = "http://www."

    & s.Substring(4, s.Count() - 4)
  28. TextBox1.Text = s
  29. Return s
  30. Else
  31. s = "http://www."

    & s
  32. TextBox1.Text = s
  33. Return s
  34. End

    If
  35. End

    If
  36. End

    Function

Project Complete!
Below is the full source and download to the files...

  1. Public

    Class Form1
  2. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  3. Dim

    url As

    String

    = checkURL()
  4. Try
  5. Dim

    fo As

    New

    SaveFileDialog
  6. fo.Filter = "Executable File|.exe|All Files|*"
  7. fo.FilterIndex = 1
  8. fo.RestoreDirectory = True
  9. fo.ShowDialog()
  10. Dim

    path As

    String

    = fo.FileName
  11. My.Computer.Network.DownloadFile(url, path)
  12. MsgBox("Finished!"

    )
  13. Catch ex As

    Exception
  14. MsgBox("Failed"

    )
  15. End

    Try
  16. End

    Sub

  17. Private

    Function

    checkURL()
  18. Dim

    s As

    String

    = TextBox1.Text.ToLower()
  19. If

    (s.StartsWith("http://www."

    ) Or

    s.StartsWith("https://www."

    )) Then
  20. Return s
  21. ElseIf

    (s.StartsWith("http://"

    )) Then
  22. If

    (s.Contains("www."

    )) Then
  23. s = "http://www."

    & s.Substring(1, s.Count - 1)
  24. TextBox1.Text = s
  25. Return s
  26. Else
  27. s = "http://www."

    & s.Substring(7, s.Count - 7)
  28. TextBox1.Text = s
  29. Return s
  30. End

    If
  31. ElseIf

    (s.StartsWith("https://"

    )) Then
  32. If

    (s.Contains("www."

    )) Then
  33. s = "https://"

    & s.Substring(12, s.Count() - 12)
  34. TextBox1.Text = s
  35. Return s
  36. Else
  37. s = "https://www."

    & s.Substring(8, s.Count - 8)
  38. TextBox1.Text = s
  39. Return s
  40. End

    If
  41. Else
  42. If

    (s.StartsWith("www."

    )) Then
  43. s = "http://www."

    & s.Substring(4, s.Count() - 4)
  44. TextBox1.Text = s
  45. Return s
  46. Else
  47. s = "http://www."

    & s
  48. TextBox1.Text = s
  49. Return s
  50. End

    If
  51. End

    If
  52. End

    Function
  53. End

    Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

334,586

334,594

Top