Malasorte
Malasorte

Reputation: 1173

Get confirmation or error after FTP upload

Using the script below, I uploaded a file to a FTP server. It worked, but would be nice if the script would also show a message box with a confirmation if the upload is successful. Or a message box displaying an error code if the upload failed. Any help please?

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

I know I should do something like this:

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

I just don't know how to put the pieces toghether.

Upvotes: 0

Views: 307

Answers (2)

filleszq
filleszq

Reputation: 151

You could try to use a Try & Catch https://msdn.microsoft.com/en-us/library/xtd0s8kd(v=vs.110).aspx

Upvotes: 1

Malasorte
Malasorte

Reputation: 1173

Ok, found a good solution to this:

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

Upvotes: 0

Related Questions