如何检查FTP连接?

是否有一种简单,快速的方法来检查FTP连接(包括主机,端口,用户名和密码)是否有效且有效? 我正在使用C#。 谢谢。

您可以尝试使用System.Net.FtpWebRequest ,然后只检查GetResponseStream方法。

所以像

 System.Net.FtpWebRequest myFTP = new System.Net.FtpWebRequest //Add your credentials and ports try { myFTP.GetResponseStream(); //set some flags } catch ex { //handle it when it is not working } 

尝试这样的事情:

  FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.google.com"); requestDir.Credentials = new NetworkCredential("username", "password"); try { WebResponse response = requestDir.GetResponse(); //set your flag } catch { } 

/ * Hola Este es el metodo que utilizo si conoces uno mejor hasmelo saber Ubirajara 100%Mexicano isc.erthal@gmail.com * /

 private bool isValidConnection(string url, string user, string password) { try { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential(user, password); request.GetResponse(); } catch(WebException ex) { return false; } return true; } 

使用System.Net.FtpWebRequest或System.Net.WebRequestMethods.Ftp使用您的登录凭据测试您的连接。 如果FTP请求因任何原因失败,将返回相应的错误消息,指出问题是什么(身份validation,无法连接等等)

这是从msdn站点到服务器的diplay文件

 public static bool DisplayFileFromServer(Uri serverUri) { // The serverUri parameter should start with the ftp:// scheme. if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } // Get the object used to communicate with the server. WebClient request = new WebClient(); // This example assumes the FTP site uses anonymous logon. request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com"); try { byte [] newFileData = request.DownloadData (serverUri.ToString()); string fileString = System.Text.Encoding.UTF8.GetString(newFileData); Console.WriteLine(fileString); } catch (WebException e) { Console.WriteLine(e.ToString()); } return true; }