如果我运行我的程序并且我的网络中不存在strUploadIP,那么整个应用程序都会被卡住
FileInfo toUpload = new FileInfo(strFile);
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(@"ftp://" + **strUploadIP** + @"/" + strUser);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.Credentials = new NetworkCredential(strUusername, strUpassword);
try
{
using (var resp = (FtpWebResponse)req.GetResponse())
{
Console.WriteLine(resp.StatusCode);
}
}
我怎样才能尝试抓住这个或什么?
答案 0 :(得分:0)
你应该添加一个try / catch来处理任何错误。
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(@"ftp://" + **strUploadIP** + @"/" + strUser);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.Credentials = new NetworkCredential(strUusername, strUpassword);
try {
using (var resp = (FtpWebResponse)req.GetResponse()) {
Console.WriteLine(resp.StatusCode);
}
} catch {
// TODO: Handle exception
}