DownloadFileAsync:需要检查是否没有互联网连接或下载失败

问题描述:

我有一段代码使用DownloadFileAsync下载文件.如果我断开互联网连接,则该按钮会单击,并且什么也没有发生.如果互联网断开以及下载失败,我想抛出一个异常.关于如何实现这一目标的任何想法 ?谢谢

I have a chunk of code that downloads a file using DownloadFileAsync. If I disconnect the internet the button clicks and nothing happens. I want to throw an exception if the internet is down and also if the download fails. Any ideas on how to achieve this ? Thanks

 Private Sub downloadFile(ByVal srcPath As String, ByVal destPath As String)
        Dim wClient As New System.Net.WebClient()
        AddHandler wClient.DownloadProgressChanged, AddressOf downloadFile_ProgressChanged
        wClient.DownloadFileAsync(New System.Uri(srcPath), destPath)
    End Sub

您可以使用Ping类检查Internet连接是否可用
You can use the Ping class to check if Internet connection is available http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.send.aspx.