2012-02-02 114 views
2

这里是解决方案,感谢古斯塔沃您的建议:下载文件使用FTP条件stament

class Program 
{ 
    static void Main(string[] args) 
    { 
     ConsoleApplication1.Program test = new Program(); 
     string Name = "SE_TEST"; 
     string[] filteredfiles = test.GetFileList(); 
     string[] files = filteredfiles; 
    foreach (string file in files) 
    { 

     bool b; 
     if (b = file.Contains(Name)) 
     { 
      test.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://" + "1.0.0.1" + "/")); 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential("sh", "SE"); 
      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 
     { 
      FtpWebRequest reqFTP; 
      string ftpserverIp = "1.0.0.1"; 

      string fileName = @"c:\downloadDir\" + file; 
      FileInfo downloadFile = new FileInfo(fileName); 
      // string uri = "ftp://1.0.0.1"; 
      FileStream outputStream = new FileStream(fileName, FileMode.Append); 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpserverIp + "/" + file)); 
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
      reqFTP.UseBinary = true; 
      reqFTP.KeepAlive = false; 
      reqFTP.Timeout = -1; 
      reqFTP.UsePassive = true; 
      reqFTP.Credentials = new NetworkCredential("sh", "SE"); 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream ftpStream = response.GetResponseStream(); 
      long cl = response.ContentLength; 
      // reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
      int bufferSize = 4096; 
      int readCount; 
      byte[] buffer = new byte[bufferSize]; 
      readCount = ftpStream.Read(buffer, 0, bufferSize); 
      Console.WriteLine("Connected: Downloading File"); 
      while (readCount > 0) 
      { 
       outputStream.Write(buffer, 0, readCount); 
       readCount = ftpStream.Read(buffer, 0, bufferSize); 
       Console.WriteLine(readCount.ToString()); 
      } 

      ftpStream.Close(); 
      outputStream.Close(); 
      response.Close(); 
      Console.WriteLine("Downloading Complete"); 
     } 
     catch (Exception ex) 
     { 
      Console.Write(ex); 
     } 

    } 
    } 
} 

它能正常工作,但不知道的性能,这是没有更好的办法做,请suggest.Anyway感谢您的建议

回答

3
+0

感谢古斯塔沃,你的意思是说,reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; – Usher 2012-02-02 05:06:39