2014-11-22 4 views
-1

FTP 사이트에 파일이있을 때 완벽하게 작동하는이 코드가 있지만 FTP 사이트에 파일이 없으면 실패합니다. 오류 II는 (문자열 파일의 파일)에 있으며 null 참조가 있다고합니다.C# Ftp 오류 - Null 참조

FTP 사이트에 파일이 없으면 어떻게 해결할 수 있습니까?이 코드는 작동합니다.

미리 감사드립니다.

내 오류 메시지 System.NullReferenceException 당신은 GetFilesList의 결과를 (테스트해야 {

  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

사용자 코드

코드

공공 무효 메인())에 의해 처리되지 않은했다 오류가 발생하면 null을 반환하므로 (GetFileList()의 catch 절에서 결과 - downloadFiles -를 null로 설정 함).

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

문제는 GetFileList에 대한 호출이 null을 반환하므로 foreach가 실패합니다.