How to Create a File Downloader in Visual Basic

Language

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.  
  18. Private Function checkURL()
  19. Dim s As String = TextBox1.Text.ToLower()
  20. If (s.StartsWith("http://www.") Or s.StartsWith("https://www.")) Then
  21. Return s
  22. ElseIf (s.StartsWith("http://")) Then
  23. If (s.Contains("www.")) Then
  24. s = "http://www." & s.Substring(1, s.Count - 1)
  25. TextBox1.Text = s
  26. Return s
  27. Else
  28. s = "http://www." & s.Substring(7, s.Count - 7)
  29. TextBox1.Text = s
  30. Return s
  31. End If
  32. ElseIf (s.StartsWith("https://")) Then
  33. If (s.Contains("www.")) Then
  34. s = "https://" & s.Substring(12, s.Count() - 12)
  35. TextBox1.Text = s
  36. Return s
  37. Else
  38. s = "https://www." & s.Substring(8, s.Count - 8)
  39. TextBox1.Text = s
  40. Return s
  41. End If
  42. Else
  43. If (s.StartsWith("www.")) Then
  44. s = "http://www." & s.Substring(4, s.Count() - 4)
  45. TextBox1.Text = s
  46. Return s
  47. Else
  48. s = "http://www." & s
  49. TextBox1.Text = s
  50. Return s
  51. End If
  52. End If
  53. End Function
  54. 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.

Add new comment