The code won't work very well because the Connect
method will only return after the connection has been successfully established, otherwise it will throw an exception.
However, there is actually a built-in timeout support for all Ftp
methods including Connect
. By default, the timeout is set to 60 seconds, but it can be changed. Try this code:
client.Timeout = 300 * 1000; // set timeout to 300 seconds (5 minutes)
client.Connect("ftp.example.com"); // connect to the FTP server
client.Login("user","password"); // log in
To handle connection error, put a try/catch (FtpException ex)
block around this code and handle errors in the catch
section.