2015-09-06 57 views
-1

我在vb.net中设计了一个下载管理器,但我不知道如何在下载开始后或者当我放置url时获取文件大小。得到vb.net中的下载文件大小

我在互联网上搜索和这里,我发现httpwebrequest类, ,但我不知道它是如何工作的,我怎样才能在我的项目中添加它。

+6

可能重复(HTTP [下载它在VB之前获取文件大小] ://stackoverflow.com/questions/32269919/get-file-size-before-download-it-in-vb) – Drarig29

+1

我看到之前,但我不知道如何使用代码'webrequest' –

+1

使用[此解决方案](http://stackoverflow.com/a/32340260/3970387)。 – Drarig29

回答

0

使用此功能:

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 

下面是使用一个简单的例子:

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

来源:https://stackoverflow.com/a/32340260/3970387