How to check FTP connection?

This might be useful.

  public async Task<bool> ConnectAsync(string host, string user, string password)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host);
            request.Credentials = new NetworkCredential(user, password);
            request.UseBinary = true;
            request.UsePassive = true;
            request.KeepAlive = false; // useful when only to check the connection.
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse) await request.GetResponseAsync();
            return true;
        }
        catch (Exception)
        {
            return false;
        }            
    }

Use either System.Net.FtpWebRequest or System.Net.WebRequestMethods.Ftp to test your connection using your login credentials. If the FTP request fails for whatever reason the appropriate error message will be returned indicating what the problem was (authentication, unable to connect, etc...)


try something like this:

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

this is the method I use, let me know if you know a better one.

/*Hola Este es el metodo que utilizo si conoces uno mejor hasmelo saber Ubirajara 100% Mexicano [email protected] */

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;
}

Tags:

C#

Connection

Ftp