0

我创建了一个将文件上传到远程 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());
        }
    }
4

2 回答 2

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);
        }
于 2013-10-23T19:19:21.740 回答
0

从我的评论来看,如果有错误,它会将它吐到控制台,但不等待 readkey,所以控制台正在消失,你永远不会在调用中捕获另一个异常,因为函数中的 catch 正在处理错误。在函数内部替换Console.WriteLineMessagebox.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;
    }
于 2013-10-23T17:36:46.560 回答