Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check FTP connection?

Tags:

c#

connection

ftp

Is there a simple, fast way to check that a FTP connection (includes host, port, username and password) is valid and working? I'm using C#. Thank you.

like image 509
Quan Mai Avatar asked Jul 13 '10 04:07

Quan Mai


2 Answers

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
{
}
like image 155
anishMarokey Avatar answered Oct 13 '22 09:10

anishMarokey


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;
}
like image 35
Ubirajara Erthal Avatar answered Oct 13 '22 10:10

Ubirajara Erthal