VB.NET测试ftp连接

时间:2014-07-06 03:21:57

标签: vb.net ftp

所以我试图将文件上传到我的ftp,虽然我希望它测试FTP详细信息是否正确。我在下面找到了这个代码,虽然它显示了“存在”,但却有效。当它连接但当它无法连接[与假的细节]时它什么都不做(我想要一个错误来表明它无法连接)

Imports System.Net

    Dim request = _
    DirectCast(WebRequest.Create _
    ("ftp://ftp.example.com/folder_here/"), FtpWebRequest)

    request.Credentials = _
    New NetworkCredential("user_here", "pass_here")

    request.Method = WebRequestMethods.Ftp.ListDirectory

    Try
        Using response As FtpWebResponse = _
        DirectCast(request.GetResponse(), FtpWebResponse)
            ' Folder exists here
            MsgBox("exists!")
        End Using

    Catch ex As WebException
        Dim response As FtpWebResponse = _
        DirectCast(ex.Response, FtpWebResponse)
        'Does not exist
        If response.StatusCode = _
        FtpStatusCode.ActionNotTakenFileUnavailable Then
            MsgBox("Doesn't exist!")
        End If
    End Try

我将如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我认为using块可能正在捕获异常。试试这个

Try
    Dim response As FtpWebResponse = _
    DirectCast(request.GetResponse(), FtpWebResponse)
        ' Folder exists here
    MsgBox("exists!")

Catch ex As WebException
    Dim response As FtpWebResponse = _
    DirectCast(ex.Response, FtpWebResponse)
    'Does not exist
    If response.StatusCode = _
    FtpStatusCode.ActionNotTakenFileUnavailable Then
        MsgBox("Doesn't exist!")
    End If
End Try