FTP上传永远不会引发错误问题

时间:2013-10-23 17:27:16

标签: c#

我创建了一个将文件上传到远程FTP服务器的应用程序。如果凭据(地址,用户名,密码等)错误,我想抛出错误。截至目前,它永远不会。我的代码出了什么问题?如果凭据正确,则会成功上传。

这是我正在使用的课程:

 public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = File.OpenRead(localFile);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

以及我在表格中调用上传功能的地方:

点击按钮=

    ftp ftpClient = new ftp(@"ftp://weburl.com/", "user", "password");
    UploadToFTP("testing/file.txt" , @"C:\file.txt");

    void UploadToFTP(string FTPfolder, string LocalFolder)
    {
        try
        {
            ftpClient.upload(FTPfolder, LocalFolder);
            Messagebox.Show("Uploaded Successfully!");
        }
        catch (Exception ex)
        {
            Messagebox.Show(ex.ToString());
        }
    }

2 个答案:

答案 0 :(得分:0)

从我的评论中如果有错误,它将它吐出到控制台,但是没有等待readkey,所以控制台正在消失,你从来没有在你的调用中捕获另一个异常,因为函数中的catch正在处理错误。在函数内部用Console.WriteLine替换Messagebox.Show以查看捕获的错误

 public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = File.OpenRead(localFile);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Messagebox.Show(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Messagebox.Show(ex.ToString()); }
        return;
    }

答案 1 :(得分:0)

重写您的upload函数正在吃异常。

我会用这种方式重写你的catch块:

        try
        {
            // ....
        }
        catch (WebException webEx)
        {
            string message = string.Empty;

            if (webEx.Response.GetType() == typeof(FtpWebResponse))
            {
                FtpWebResponse response = (FtpWebResponse)webEx.Response;
                message = string.Format("{0} - {1}", response.StatusCode, response.StatusDescription);
            } else
            {
                message = webEx.Message;
            }

            throw new Exception(message);
        }