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.
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...)
Well, the obvious answer is to issue a FTP command and see if you get an answer. Which may seem a bit vague, but your question is a bit vague as you don't say WHAT you mean by a FTP connection: is it through a 3rd party library, FTPWebRequest, or are you handling the protocol yourself using an TCP/IP connection?
Of course that just changes the way you issue the command: the basic answer is the same, give a command and see what it says in response.
You could try using
System.Net.FtpWebRequest
and then just check the GetResponseStream
method.
So something like
System.Net.FtpWebRequest myFTP = new System.Net.FtpWebRequest
//Add you credentials and ports
try
{
myFTP.GetResponseStream();
//set some flag
}
catch ex
{
//handle it not working
}
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
{
}