C# FTP上载从不引发错误问题

C# FTP上载从不引发错误问题,c#,C#,我创建了一个将文件上传到远程FTP服务器的应用程序。如果凭据(地址、用户名、密码等)错误,我想抛出一个错误。到目前为止,它从来没有这样做过。我的代码有什么问题?当凭据正确时,它将成功上载 以下是我正在使用的类: public void upload(string remoteFile, string localFile) { try { ftpRequest = (FtpWebRequest)FtpWebRequest.Creat

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

根据我的评论,如果有错误,它会将其发送到控制台,但没有等待readkey,因此控制台正在消失,您永远不会在调用中捕获另一个异常,因为函数中的捕获正在处理错误。在函数内部,用消息框替换控制台。用消息框写入行。显示,查看捕获的错误

 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;
    }

重写你的
上传
函数正在吞噬异常

我会这样重写您的捕获块:

        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);
        }

你永远不会看到messagebox,因为你正在吞咽
上传
函数中的错误。如果有错误,它会将错误吐到控制台,但不会等待readkey,因此控制台正在消失,你永远不会在调用中捕获另一个异常,因为函数中的捕获正在处理错误。在函数内部,将
控制台。WriteLine
替换为
消息框。显示