文件上传应用程序卡住了

时间:2013-05-14 09:26:11

标签: c# file upload ftpwebrequest

如果我运行我的程序并且我的网络中不存在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);
                        }
                    }

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

1 个答案:

答案 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
    }