Search code examples
.netvb.nethttpwebrequestfilesize

get the downloading file size in vb.net


I designed a download manager in vb.net, but I don't know how to get file size after downloading started, or when I put the url.

I searched on internet and here, and I found the httpwebrequest class, but I don't know how does it work and how can I add it in my project.


Solution

  • Use this function :

    Public Function GetDownloadSize(ByVal URL As String) As Long
        Dim r As Net.WebRequest = Net.WebRequest.Create(URL)
        r.Method = Net.WebRequestMethods.Http.Head
        Using rsp = r.GetResponse()
            Return rsp.ContentLength
        End Using
    End Function
    

    Here's an easy example of use :

    MsgBox(Math.Round(GetDownloadSize("http://www.foo.com/file.mp3") / 1024 / 1024, 2) & " MB")
    

    Source : https://stackoverflow.com/a/32340260/3970387