0

如果我运行我的程序并且 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);
                        }
                    }

我怎样才能尝试抓住这个或什么?

4

1 回答 1

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
    }
于 2013-05-14T09:34:11.883 回答