2013-10-23 36 views
0

我創建了一個應用程序,可以將文件上傳到遠程FTP服務器。如果證書(地址,用戶名,密碼等)錯誤,我想拋出一個錯誤。截至目前,它從未如此。我的代碼有什麼問題?當證書正確時,它會成功上傳。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()); 
     } 
    } 
+0

你永遠不會看到一個消息,因爲你吞的'upload'功能 – lincolnk

+0

貌似如果有錯誤你的錯誤,它將它分配給CONSOL,但不等待READY鍵,所以CONSOL正在消失,你從來沒有在你的調用中捕獲到另一個異常,因爲函數中的catch處理錯誤。函數內部用'Messagebox.Show'替換'Console.WriteLine' – user2140261

回答

0

從我的評論如果出現錯誤,它將其分發給控制檯,但不等待讀取鍵,所以控制檯正在消失,您從未在您的調用中捕獲到另一個異常,因爲函數中的catch處理e錯誤。在函數內部用Messagebox.Show取代Console.WriteLine看到捕獲錯誤的

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