FTP 上传后得到确认或错误

Get confirmation or error after FTP upload

使用下面的脚本,我将文件上传到 FTP 服务器。它有效,但如果脚本在上传成功时也显示一个带有确认信息的消息框,那就太好了。或者,如果上传失败,则显示错误代码的消息框。有什么帮助吗?

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    client.UploadFile("ftp://example.com/target.txt", "STOR", localFilePath);
}

我知道我应该这样做:

byte[] responseArray = client.UploadFile("ftp://example.com/target.txt", localFilePath);
string s = System.Text.Encoding.ASCII.GetString(responseArray);

我就是不知道怎么拼凑。

您可以尝试使用 Try & Catch https://msdn.microsoft.com/en-us/library/xtd0s8kd(v=vs.110).aspx

好的,找到了一个很好的解决方案:

bool exception = false;

            try
            {

                using (WebClient client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(FtpUser.Text, FtpPass.Text);
                    client.UploadFile("ftp://example.com/file.txt", "STOR", MyFilePath);

                }

            }
            catch (Exception ex)
            {
                exception = true;
                MessageBox.Show(ex.Message);
            }


            if(!exception){
                MessageBox.Show("Upload worked!");
            }