VB 2013 DownloadFile错误/例外

时间:2014-07-17 17:07:41

标签: vb.net downloadfile

尝试使用My.Computer.Network.DownloadFile从VB 2013 Express中的FTP服务器获取文件。当文件存在时,一切都很好。但是,当找不到文件时,我无法捕获结果。使用Try-Catch但它永远不会击中Catch。它会在您的应用程序中出现"未处理的异常......"错误。

非常感谢任何帮助!

Try
   My.Computer.Network.DownloadFile(server_name, file_name, user_name, password, False, 500, True)
Catch ex As ArgumentException
   MsgBox(ex.GetType().ToString())
Catch ex As TimeoutException
   MsgBox(ex.GetType().ToString()) 'Label1.Text = ex
End Try

1 个答案:

答案 0 :(得分:2)

看看the documentation - 你几乎没有捕获DownloadFile可能抛出的所有可能的异常类型。

The following conditions may cause an exception to be thrown:
  The drive name is not valid (ArgumentException).
  destinationFileName ends with a trailing slash (ArgumentException).
  overwrite is set to False and the destination file already exists (IOException).
  The server does not respond within the specified connectionTimeout (TimeoutException).
  The authentication fails (SecurityException).
  User lacks necessary permissions (SecurityException).
  The request is denied by the website (WebException).

尝试

Try
   My.Computer.Network.DownloadFile(server_name, file_name, user_name, password, False, 500, True)
Catch ex As Exception
   MsgBox(ex.GetType().ToString())

看看发生了什么。