2012-08-16 39 views
1

我正在创建一个需要FTP到目录以检索文件列表的C#应用​​程序。下面的代码工作得很好。但是,我正在FTP的文件夹包含大约92,000个文件。此代码不会按照我希望的方式处理该大小的文件列表。使用FTP从目录中检索特定文件

我只查找以字符串“c-”开头的文件。在做了一些研究之后,我甚至不知道如何着手解决这个问题。有没有什么办法可以修改这个现有的代码,以便它只检索那些文件?

public string[] getFileList() { 
    string[] downloadFiles; 
    StringBuilder result = new StringBuilder(); 
    FtpWebRequest reqFTP; 

    try { 
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpHost)); 
     reqFTP.UseBinary = true; 
     reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass); 
     reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
     WebResponse response = reqFTP.GetResponse(); 
     StreamReader 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); 
     reader.Close(); 
     response.Close(); 
     return result.ToString().Split('\n'); 
    } 
    catch (Exception ex) { 
     System.Windows.Forms.MessageBox.Show(ex.Message); 
     downloadFiles = null; 
     return downloadFiles; 
    } 
} 
+0

你看过正则表达式吗? – 2012-08-16 13:50:08

+1

@RobertH - 我确实认为正则表达式可能是要走的路,但问题实际上是检索文件列表来解析正则表达式。它只会挂起。 – 2012-08-16 13:51:54

+1

我对FTP本身并不熟悉,但假设这一行包含了你的文件名(可能还有额外的东西),那么根据空格或任何控制字符存在,然后将结果文件名通过正则表达式并将其添加到结果,如果它匹配的正则表达式? – 2012-08-16 13:59:41

回答

1

我觉得LIST不支持通配符搜索,实际上它可能是从不同的FTP平台有所不同,取决于命令支持

,你将需要在FTP目录下载的所有文件名使用LIST,可能是以异步的方式。

0

下面是沿着类似的路线的替代实现。我已经用多达1000个ftp文件测试过了,它可能适用于你。完整的源代码可以找到here

public List<ftpinfo> browse(string path) //eg: "ftp.xyz.org", "ftp.xyz.org/ftproot/etc" 
    { 
     FtpWebRequest request=(FtpWebRequest)FtpWebRequest.Create(path); 
     request.Method=WebRequestMethods.Ftp.ListDirectoryDetails; 
     List<ftpinfo> files=new List<ftpinfo>(); 

     //request.Proxy = System.Net.WebProxy.GetDefaultProxy(); 
     //request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; 
     request.Credentials = new NetworkCredential(_username, _password); 
     Stream rs=(Stream)request.GetResponse().GetResponseStream(); 

     OnStatusChange("CONNECTED: " + path, 0, 0); 

     StreamReader sr = new StreamReader(rs); 
     string strList = sr.ReadToEnd(); 
     string[] lines=null; 

     if (strList.Contains("\r\n")) 
     { 
      lines=strList.Split(new string[] {"\r\n"},StringSplitOptions.None); 
     } 
     else if (strList.Contains("\n")) 
     { 
      lines=strList.Split(new string[] {"\n"},StringSplitOptions.None); 
     } 

     //now decode this string array 

     if (lines==null || lines.Length == 0) 
      return null; 

     foreach(string line in lines) 
     { 
      if (line.Length==0) 
       continue; 
      //parse line 
      Match m= GetMatchingRegex(line); 
      if (m==null) { 
       //failed 
       throw new ApplicationException("Unable to parse line: " + line); 
      } 

      ftpinfo item=new ftpinfo(); 
      item.filename = m.Groups["name"].Value.Trim('\r'); 
      item.path = path; 
      item.size = Convert.ToInt64(m.Groups["size"].Value); 
      item.permission = m.Groups["permission"].Value; 
      string _dir = m.Groups["dir"].Value; 
      if(_dir.Length>0 && _dir != "-") 
      { 
       item.fileType = directoryEntryTypes.directory; 
      } 
      else 
      { 
       item.fileType = directoryEntryTypes.file; 
      } 

      try 
      { 
       item.fileDateTime = DateTime.Parse(m.Groups["timestamp"].Value); 
      } 
      catch 
      { 
       item.fileDateTime = DateTime.MinValue; //null; 
      } 

      files.Add(item); 
     } 

     return files; 
    } 
相关问题