2017-01-03 21 views
0

使用下面的腳本,我上傳了一個文件到FTP服務器。它的工作原理,但如果腳本也會顯示一個消息框,並確認上傳是否成功,那將會很好。或者如果上傳失敗,則顯示錯誤代碼的消息框。請幫忙嗎?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); 

我只是不知道如何把碎片toghether。

回答

0

好了,找到了一個很好的解決這個:

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