|
-
Sep 1st, 2018, 06:32 AM
#1
Thread Starter
Junior Member
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!
-
Sep 1st, 2018, 07:23 AM
#2
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.
-
Sep 1st, 2018, 07:26 AM
#3
Thread Starter
Junior Member
Re: VB.NET: Download executable file from Dropbox
-
Sep 1st, 2018, 08:05 AM
#4
Re: VB.NET: Download executable file from Dropbox
Here are what your three options look like:
vb.net Code:
Imports System.ComponentModel Imports System.Net Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Using client As New WebClient 'This call will block until the file has been downloaded. 'This means that the UI will freeze for that period. client.DownloadFile("source address here", "destination path here") End Using End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim client As New WebClient 'This enables a notification when an asynchronous file download completes. AddHandler client.DownloadFileCompleted, AddressOf client_DownloadFileCompleted 'This call will not block while the file downloads so the UI will not freeze. 'This means that the file will not be available immediately. client.DownloadFileAsync(New Uri("source address here"), "destination path here") End Sub Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Dim client = DirectCast(sender, WebClient) client.Dispose() 'The downloaded file is now available. 'If you want the path of that file then pass it to the "userToken" parameter 'of DownloadFileAsync and get it back from "e.UserState" in this event handler. End Sub Private Async Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Using client As New WebClient 'This call will not block while the file downloads so the UI will not freeze. 'The Task it creates will be awaited here though. 'It behaves ostensibly like a synchronous call that won't freeze the UI. Await client.DownloadFileTaskAsync("source address here", "destination path here") End Using End Sub 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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|