vb.net 检查FTP是否在线/离线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17863600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:24:18  来源:igfitidea点击:

Check if FTP is online/offline

vb.netvisual-studiopingftp-server

提问by Janno

What is the best way to determine if a ftp server is online or offline in Visual Basic? I have tried many different ways of pinging the ftp server, but whatever I try, I get this error:

在 Visual Basic 中确定 ftp 服务器是联机还是脱机的最佳方法是什么?我尝试了许多不同的方法来 ping ftp 服务器,但是无论我尝试什么,我都会收到此错误:

An exception occurred during a Ping request.

Is this easily fixable? Or is there a better method than what I'm using?

这很容易修复吗?或者有没有比我正在使用的方法更好的方法?

采纳答案by Don Thomas Boyle

Using webrequest, try this code i found

使用 webrequest,试试我发现的这段代码

Hi, this should work fine:

嗨,这应该可以正常工作:

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

..the idea is that we use FtpWebRequest class and pass the folder name with a trailing slash "/", if the folder is found then the response will be processed fine inside Try-Catch block, if the folder could not be found, we handle the exception controlling with statusCode (ActionNotTakenFileUnavailable) to determine if absence of folder causes exception. That should work fine.

..这个想法是我们使用 FtpWebRequest 类并使用尾部斜杠“/”传递文件夹名称,如果找到文件夹,那么响应将在 Try-Catch 块内被很好地处理,如果找不到文件夹,我们使用 statusCode (ActionNotTakenFileUnavailable) 处理异常控制以确定文件夹的缺失是否导致异常。那应该可以正常工作。

Sources First answer

来源第一个答案

--------------- Please also Try----------------------

---------------请也试试--------------

Public Function CheckIfFtpFileExists(ByVal fileUri As String) As Boolean
   Dim request As FtpWebRequest = WebRequest.Create(fileUri)
   request.Credentials = New NetworkCredential("username", "password")
   request.Method = WebRequestMethods.Ftp.GetFileSize
      Try
       Dim response As FtpWebResponse = request.GetResponse()
       ' THE FILE EXISTS
   Catch ex As WebException
        Dim response As FtpWebResponse = ex.Response
        If FtpStatusCode.ActionNotTakenFileUnavailable = response.StatusCode Then
            ' THE FILE DOES NOT EXIST
            Return False
        End If
    End Try
    Return True
End Function

Get's called like this:

Get 是这样调用的:

If CheckIfFtpFileExists("ftp://ftp.domain.com/filename.txt") Then
    ' Do something
End If

Sources

来源

回答by PaulG

can you not just do this:

你能不能不这样做:

    If My.Computer.Network.Ping("IP HERE") Then
        ' Success
    Else
        'fail
    End If

Thanks

谢谢

Paul

保罗

回答by Steven Liekens

Typically you write your code in a way that assumes that everything is perfect, then add error handling for cases where the connection fails.

通常,您以假设一切都完美的方式编写代码,然后为连接失败的情况添加错误处理。

Verifying whether the server is online beforehand does not guarantee that it will stay that way for the duration of the user session.

事先验证服务器是否在线并不能保证它会在用户会话期间保持在线状态。

回答by user1838061

- (void)newBuddyOnline:(NSString *)buddyName {

[onlineBuddies addObject:buddyName];
[self.TableView reloadData];

}

- (void)buddyWentOffline:(NSString *)buddyName {

[onlineBuddies removeObje`
`ct:buddyName];
[self.TableView reloadData];
}

This may helps .

这可能会有所帮助。