2016-06-14 56 views
0

我想下载某个ftp文件夹的所有文件(它会变化=>变量),并将所有内容显示到一个ListView中。如何使用c#winforms从ftp文件夹下载所有文件?

如何使用c#winforms实现此目的?

我一直在使用这种尝试:

public void download(string remoteFile, string localFile) 
{ 
    try 
    { 
     /* Create an FTP Request */ 
     ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile); 
     /* Log in to the FTP Server with the User Name and Password Provided */ 
     ftpRequest.Credentials = new NetworkCredential(user, pass); 
     /* When in doubt, use these options */ 
     ftpRequest.UseBinary = true; 
     ftpRequest.UsePassive = true; 
     ftpRequest.KeepAlive = true; 
     /* Specify the Type of FTP Request */ 
     ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; 
     /* Establish Return Communication with the FTP Server */ 
     ftpResponse = (FtpWebResponse)ftpRequest.GetResponse(); 
     /* Get the FTP Server's Response Stream */ 
     ftpStream = ftpResponse.GetResponseStream(); 
     /* Open a File Stream to Write the Downloaded File */ 
     FileStream localFileStream = new FileStream(localFile, FileMode.Create); 
     /* Buffer for the Downloaded Data */ 
     byte[] byteBuffer = new byte[bufferSize]; 
     int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); 
     /* Download the File by Writing the Buffered Data Until the Transfer is Complete */ 
     try 
     { 
      while (bytesRead > 0) 
      { 
       localFileStream.Write(byteBuffer, 0, bytesRead); 
       bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize); 
      } 
     } 
     catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
     /* Resource Cleanup */ 
     localFileStream.Close(); 
     ftpStream.Close(); 
     ftpResponse.Close(); 
     ftpRequest = null; 
    } 
    catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
    return; 
} 

这将只下载1个指定的文件。

+0

更新我的问题 –

回答

2

为了能够将所有文件从FTP目录下载到本地文件夹,您必须列出远程目录中的所有文件,然后逐一下载它们。您可以使用下面的代码做相同的:

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; 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
      reqFTP.Proxy = null; 
      reqFTP.KeepAlive = false; 
      reqFTP.UsePassive = false; 
      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://" + ftpServerIP + "/" + remoteDir + "/" + file; 
      Uri serverUri = new Uri(uri); 
      if (serverUri.Scheme != Uri.UriSchemeFtp) 
      { 
       return; 
      }  
      FtpWebRequest reqFTP;     
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + remoteDir + "/" + file));         
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);     
      reqFTP.KeepAlive = false;     
      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(localDestnDir + "\" + 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"); 
     } 
    } 

参考:https://social.msdn.microsoft.com/Forums/en-US/079fb811-3c55-4959-85c4-677e4b20bea3/downloading-all-files-in-directory-ftp-and-c?forum=ncl

相关问题