SolvedGetting File Size

Posts 1–8 of 8 · Page 1 of 1
Getting File Size
Hi, I'm trying to get a working progress bar for a file download. To do this I was planning on getting the size of the file and using that as the max value so that the prog bar would fill as more of the file is downloaded. However I don't know how to file the size of an remote file. Can anyone help me either find the size of a remote file or introduce me to a different method of using a progress bar.
Thank you
What protocol are you using for the download? FTP, HTTP?
HTTP?
Code:
My.Computer.Network.DownloadFile(URL, Package)
Sorry I'm not too sure as this is something I've never tried before.
I'd recommend using a WebClient if you haven't done much of this before, it removes a lot of the technical side of things (much as My.Computer.Network.DownloadFile does, but has more capabilities)

You'll need to import the System.Net namespace
Code:
Imports System.Net
And then create a new WebClient
Code:
Private WithEvents wClient As New WebClient()
Note that I declared it "WithEvents" because we're going to handle one of its events to update your progressbar; DownloadProgressChanged.

Here's the handler for that event:

Code:
Private Sub wClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles wClient.DownloadProgressChanged
    If Me.ProgressBar1.Maximum <> e.TotalBytesToReceive Then
        Me.ProgressBar1.Maximum = e.TotalBytesToReceive
    End If

    Me.ProgressBar1.Value = e.BytesReceived
End Sub
Basically when you start a download the Maximum value of your progress bar will be set and the value updated as the progress changes. You may find its better to scale the values when you start getting to bigger files (note that the fields are measured in BYTES, 1GB is ~1073741824 bytes), just divide the fields by 1024 to measure the progress in kilobytes, or by 1024 * 1024 to measure in MB...etc.

You may also want to handle the DownloadCompleted event to let the user know when the download is completed:

Code:
    Private Sub wClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wClient.DownloadFileCompleted
        MessageBox.Show("File has finished downloading!", "Success: File Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.ProgressBar1.Value = 0 'reset the progressbar
    End Sub
To download a file, just call wClient's "DownloadFile" method:
Code:
wClient.DownloadFile(New Uri("http://example.com/somefile.zip"), "C:\savehere\somefile.zip")
This may not work too well with DownloadFile because the form will lock up during the synchronous download. Consider putting the following code in your form's "Load" event:

Code:
Control.CheckForIllegalCrossThreadCalls = False
And instead of calling "DownloadFile", call "DownloadFileAsync"

Code:
wClient.DownloadFileAsync(New Uri("http://example.com/somefile.zip"), "C:\savehere\somefile.zip")
For future reference, do note that setting "CheckForIllegalCrossThreadCalls" is a bad idea in more complex applications. However, I doubt you're going to be doing much asynchronous programming in this application and the thread-safe version of this code can be a bit overwhelming when you just want to download a file and report the progress.
Wow thanks, I wasn't expecting this much
I think I understand most of it I'm just a little confused as to where it all goes in the program and I'm not sure if what I have done is right.

BTW the project is an patcher and version manager for minecraft. This part will download whatever version the user types into TextBox1, into the folder and name according to the version that it is. the files are normally around 3-5Mb

I have this bit for the download button:
Code:
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim URL As String = "http://assets.minecraft.net/" + TextBox1.Text + "/minecraft.jar"
        Dim Package As String = "C:\Users\" + SystemInformation.UserName + "\AppData\Roaming\.minecraft\bin\" + TextBox1.Text + ".jar"
        wClient.DownloadFile(New Uri(URL), Package)
    End Sub

Then I have these bits which I'm unsure about:
Code:
Private WithEvents wClient As New WebClient()

    Private Sub wClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles wClient.DownloadProgressChanged
        If Me.ProgressBar1.Maximum <> e.TotalBytesToReceive Then
            Me.ProgressBar1.Maximum = e.TotalBytesToReceive
        End If
        Me.ProgressBar1.Value = e.BytesReceived
    End Sub
    
    Private Sub wClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wClient.DownloadFileCompleted
        MessageBox.Show("File has finished downloading!", "Success: File Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.ProgressBar1.Value = 0 'reset the progressbar
    End Sub

When I download a file it downloads fine but the program becomes unresponsive while the download is in progress and the progress bar does not change and you don't get the message saying the download has finished but I'm sure that's just me getting things wrong.
Quote Originally Posted by Genoble View Post
Wow thanks, I wasn't expecting this much
I think I understand most of it I'm just a little confused as to where it all goes in the program and I'm not sure if what I have done is right.

BTW the project is an patcher and version manager for minecraft. This part will download whatever version the user types into TextBox1, into the folder and name according to the version that it is. the files are normally around 3-5Mb

I have this bit for the download button:
Code:
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim URL As String = "http://assets.minecraft.net/" + TextBox1.Text + "/minecraft.jar"
        Dim Package As String = "C:\Users\" + SystemInformation.UserName + "\AppData\Roaming\.minecraft\bin\" + TextBox1.Text + ".jar"
        wClient.DownloadFile(New Uri(URL), Package)
    End Sub

Then I have these bits which I'm unsure about:
Code:
Private WithEvents wClient As New WebClient()

    Private Sub wClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles wClient.DownloadProgressChanged
        If Me.ProgressBar1.Maximum <> e.TotalBytesToReceive Then
            Me.ProgressBar1.Maximum = e.TotalBytesToReceive
        End If
        Me.ProgressBar1.Value = e.BytesReceived
    End Sub
    
    Private Sub wClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wClient.DownloadFileCompleted
        MessageBox.Show("File has finished downloading!", "Success: File Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.ProgressBar1.Value = 0 'reset the progressbar
    End Sub

When I download a file it downloads fine but the program becomes unresponsive while the download is in progress and the progress bar does not change and you don't get the message saying the download has finished but I'm sure that's just me getting things wrong.
Here's *roughly* how it should look like.

Code:
Imports System.Net
'any other Imports System.* go here

Public Partial Class Form1
    Private WithEvents wClient As New WebClient()

    Private Sub wClient_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles wClient.DownloadProgressChanged
        If Me.ProgressBar1.Maximum <> e.TotalBytesToReceive Then
            Me.ProgressBar1.Maximum = e.TotalBytesToReceive
        End If
        Me.ProgressBar1.Value = e.BytesReceived
    End Sub
    
    Private Sub wClient_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles wClient.DownloadFileCompleted
        MessageBox.Show("File has finished downloading!", "Success: File Complete", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.ProgressBar1.Value = 0 'reset the progressbar
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim URL As String = "http://assets.minecraft.net/" + TextBox1.Text + "/minecraft.jar"
        Dim Package As String = "C:\Users\" + SystemInformation.UserName + "\AppData\Roaming\.minecraft\bin\" + TextBox1.Text + ".jar"
        wClient.DownloadFileAsync(New Uri(URL), Package)
    End Sub  

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
    'any of your other methods would go somewhere in this section.
End Class
Note that I changed "DownloadFile" to "DownloadFileAsync" as I mentioned previously.
Works a treat! Thanks very much, not only does the progress bar and everything work but downloads are now much quicker and don't make the program unresponsive @Jason you're a genius!
No worries man.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us