Results 1 to 4 of 4

Thread: VB.NET: Download executable file from Dropbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Exclamation VB.NET: Download executable file from Dropbox

    Hello everyone,
    I have some issues with my vb code.
    What I'm trying to do is: Download an .exe file from dropbox.
    When it has finished to download the file weighs 0 bytes


    My code:

    Code:
      Using client = New System.Net.WebClient
                client.DownloadFileAsync(New Uri("https://www.dropbox.com/s/ddsavgmfwvjqgtr/testfile.exe?dl=1"), "C:\test.exe")
            End Using


    Does someone know what I wrote wrong ?
    Thanks in advance!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET: Download executable file from Dropbox

    You clearly don't know how DownloadFileAsync works. If you don't actually want an asynchronous download then just call DownloadFile. If you do want an asynchronous download then read the documentation for DownloadFileAsync and learn how it works. VS has a Help menu for a reason and you can also just click the method in the code window and press F1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2018
    Posts
    16

    Re: VB.NET: Download executable file from Dropbox

    Okay, I'll take a look.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET: Download executable file from Dropbox

    Here are what your three options look like:
    vb.net Code:
    1. Imports System.ComponentModel
    2. Imports System.Net
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    7.         Using client As New WebClient
    8.             'This call will block until the file has been downloaded.
    9.             'This means that the UI will freeze for that period.
    10.             client.DownloadFile("source address here", "destination path here")
    11.         End Using
    12.     End Sub
    13.  
    14.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    15.         Dim client As New WebClient
    16.  
    17.         'This enables a notification when an asynchronous file download completes.
    18.         AddHandler client.DownloadFileCompleted, AddressOf client_DownloadFileCompleted
    19.  
    20.         'This call will not block while the file downloads so the UI will not freeze.
    21.         'This means that the file will not be available immediately.
    22.         client.DownloadFileAsync(New Uri("source address here"), "destination path here")
    23.     End Sub
    24.  
    25.     Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs)
    26.         Dim client = DirectCast(sender, WebClient)
    27.  
    28.         client.Dispose()
    29.  
    30.         'The downloaded file is now available.
    31.         'If you want the path of that file then pass it to the "userToken" parameter
    32.         'of DownloadFileAsync and get it back from "e.UserState" in this event handler.
    33.     End Sub
    34.  
    35.     Private Async Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    36.         Using client As New WebClient
    37.             'This call will not block while the file downloads so the UI will not freeze.
    38.             'The Task it creates will be awaited here though.
    39.             'It behaves ostensibly like a synchronous call that won't freeze the UI.
    40.             Await client.DownloadFileTaskAsync("source address here", "destination path here")
    41.         End Using
    42.     End Sub
    43.  
    44. End Class
    The first option is a synchronous download. That means that you call the DownloadFile method and it won't return until the file finishes downloading. All the work occurs on the UI thread so your app will freeze while the downloading occurs. Obviously having your UI freeze for any appreciable time is not a good thing.

    For that reason, downloading asynchronously is probably a better option. You could execute a method on a secondary thread and make a synchronous call there but I haven't shown that. The DownloadFileAsync method has been around since the beginning and uses the old-style async pattern. As you can see, there's a little bit of complexity. Actually, I just remembered that (if I'm not mistaken) that DownloadFileCompleted event is raised on a secondary thread, so that adds some more complexity if you want to update the UI at that stage.

    The DownloadFileTaskAsync method uses the new async pattern, which is based on the Tasks Parallel Library (TPL) and the new Async/Await keywords. This option gives you the benefit of not freezing your UI but making your code almost as simple as making a synchronous call. You simply need to add the Await keyword before the call and the Async keyword to the calling method declaration.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width