首页 > 解决方案 > 使用 WebClient 下载在 mscorlib.dll 中发生抛出错误“System.Reflection.TargetInvocationException”

问题描述

我正在尝试使用以下代码从 url 下载文件:

Private Sub getPatch()
    Using client As New WebClient
        Dim url As String = My.Settings.host & "/update.zip"
        If Directory.Exists(Replace(fName, "\update.zip", "")) Then Directory.Delete(Replace(fName, "\update.zip", ""), True)
        Directory.CreateDirectory(Replace(fName, "\update.zip", ""))

        AddHandler client.DownloadProgressChanged, AddressOf ProgressChanged
        AddHandler client.DownloadFileCompleted, AddressOf DownloadFileCompleted
        client.DownloadFileAsync(New Uri(url), fName)
    End Using
End Sub

Public Sub ProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
    Try
        If e.ProgressPercentage = 100 Then Exit Sub
        ProgressBar1.Value = e.ProgressPercentage
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Public Sub DownloadFileCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadDataCompletedEventArgs)
    If Not e.Cancelled AndAlso e.Error Is Nothing Then
        MessageBox.Show("Download success")
        extractZip(fName)
    Else
        MessageBox.Show("Download failed")
    End If
End Sub

每当下载完成(文件成功保存到我的驱动器),它总是抛出错误

mscorlib.dll 中发生了“System.Reflection.TargetInvocationException”类型的未处理异常附加信息:调用目标引发了异常。

我尝试调试它,但它从不触发 DownloadComplete 方法。

出现错误消息后,vs 转到此屏幕

标签: vb.netwebclient

解决方案


推荐阅读