当与 FTP 服务器失去通信时不要停止程序

Do not stop the program when communication is lost with the FTP server

我有一个连接到 FTP 服务器的程序,但我有一个问题,当连接丢失时程序停止。

我想要的是,如果我失去与服务器的连接,程序会继续尝试连接到服务器,并且在连接时继续正常运行

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("...");
request.Method = WebRequestMethods.Ftp.DownloadFile;

request.Credentials = new NetworkCredential (user,pass);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

我该怎么办?

谢谢大家!

您应该将代码包装在 try-catch 块中。