【问题标题】:VB 2013 DownloadFile Error/Exception [closed]VB 2013 下载文件错误/异常
【发布时间】:2014-09-08 16:20:56
【问题描述】:

尝试使用 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

【问题讨论】:

  • 任何代码将不胜感激
  • 逐行调试,你会看到异常是在哪里产生的。我的猜测是返回的对象是 null (这不会导致异常),但是你尝试用它做一些事情,确实如此。
  • DownloadFile 命令不返回任何内容,成功或失败。该函数只是获取指定的文件并将其下载到使用提供的用户名和密码指定的位置。
  • 这是代码:尝试 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

标签: vb.net downloadfile


【解决方案1】:

看看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())

看看发生了什么。

【讨论】:

    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 2022-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    相关资源
    最近更新 更多