2014-11-22 59 views
-1

我有這個代碼,它在FTP站點上有文件時工作完美,但是當FTP站點上沒有文件時它失敗。 錯誤二得到是在foreach(文件中的字符串文件)它說有空引用。C#Ftp錯誤 - 空引用

我該如何解決這個問題,以便如果FTP站點上沒有文件,此代碼將可以正常工作。

在此先感謝。

我的錯誤信息 System.NullReferenceException是未處理由用戶代碼

代碼

公共無效的主要(){

  String[] files = GetFileList(); 
      foreach (string file in files) 
      { 
       Download(file); 
      } 
     } 

     public string[] GetFileList() 
     { 
      string[] downloadFiles; 
      StringBuilder result = new StringBuilder(); 
      WebResponse response = null; 
      StreamReader reader = null; 
      try 
      { 
       //FtpWebRequest reqFTP; 
       WebRequest reqFTP; 
       reqFTP = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + Dts.Variables["strHost"].Value+"/")); 
       //reqFTP.UseBinary = true; 
       String FTPUser = (String)Dts.Variables["strUserName"].Value; 
       String FTPPwd = (String)Dts.Variables["strPassword"].Value; 
       reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPwd); 
       reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
       reqFTP.Proxy = null; 

       //reqFTP.KeepAlive = true; 
       //reqFTP.UsePassive = true; 
       response = reqFTP.GetResponse(); 
       reader = new StreamReader(response.GetResponseStream()); 
       string line = reader.ReadLine(); 
       while (line != null) 
       { 
        result.Append(line); 
        result.Append("\n"); 
        line = reader.ReadLine(); 
       } 
       // to remove the trailing '\n' 
       result.Remove(result.ToString().LastIndexOf('\n'), 1); 
       return result.ToString().Split('\n'); 
      } 
      catch (Exception ex) 
      { 
       if (reader != null) 
       { 
        reader.Close(); 
       } 
       if (response != null) 
       { 
        response.Close(); 
       }     
       downloadFiles = null; 
       return downloadFiles; 
      } 

     } 

     private void Download(string file) 
     { 
      try 
      { 
       string uri = "ftp://" + Dts.Variables["strHost"].Value + "/" + file; 
       Uri serverUri = new Uri(uri); 
       if (serverUri.Scheme != Uri.UriSchemeFtp) 
       { 
        return; 
       } 
       WebRequest reqFTP; 
       //FtpWebRequest reqFTP; 
       reqFTP = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + Dts.Variables["strHost"].Value + "/" + file)); 
       String FTPUser = (String)Dts.Variables["strUserName"].Value; 
       String FTPPwd = (String)Dts.Variables["strPassword"].Value; 
       reqFTP.Credentials = new NetworkCredential(FTPUser, FTPPwd); 
       //reqFTP.KeepAlive = true; 
       reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
       //reqFTP.UseBinary = true; 
       reqFTP.Proxy = null; 
       //reqFTP.UsePassive = false; 
       FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
       Stream responseStream = response.GetResponseStream(); 
       FileStream writeStream = new FileStream(Dts.Variables["strLocalFolder"].Value + "\\" + file, FileMode.Create); int Length = 2048; 
       Byte[] buffer = new Byte[Length]; 
       int bytesRead = responseStream.Read(buffer, 0, Length); 
       while (bytesRead > 0) 
       { 
        writeStream.Write(buffer, 0, bytesRead); 
        bytesRead = responseStream.Read(buffer, 0, Length); 
       } 
       writeStream.Close(); 
       response.Close(); 
      } 

      catch (WebException wEx) 
      { 
       MessageBox.Show(wEx.Message, "Download Error"); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Download Error"); 
      } 

     } 

回答

1

您需要測試GetFilesList的結果()爲null,因爲如果發生錯誤(在GetFileList()的catch子句中將結果 - downloadFiles - 設置爲null),則返回null。

 public void Main() 
     {    
      String[] files = GetFileList(); 
      if (files != null) // add this line 
      { 
       foreach (string file in files) 
       { 
        Download(file); 
       } 
      } 
0

問題是您對GetFileList的調用返回null,從而導致foreach失敗。