2015-07-20 72 views
3

我想要的FTP文件夹的内容下载到使用在计算器上一本例中的本地文件夹:
Downloading a list of files from ftp to local folder using c#?检索列表,无需额外的细节

我的代码此刻是:

public void DownloadFilesFromFTP(string localFilesPath, string remoteFTPPath) 
{ 
    remoteFTPPath = "ftp://" + Hostname + remoteFTPPath; 
    var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath); 

    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; 
    request.Credentials = new NetworkCredential(Username, Password); 
    request.Proxy = null; 

    FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 
    Stream responseStream = response.GetResponseStream(); 
    StreamReader reader = new StreamReader(responseStream); 
    List<string> directories = new List<string>(); 

    string line = reader.ReadLine(); 

    while (!string.IsNullOrEmpty(line)) 
    { 
     directories.Add(line); 
     line = reader.ReadLine(); 
    } 
    reader.Close(); 

    using (WebClient ftpClient = new WebClient()) 
    { 
     ftpClient.Credentials = new System.Net.NetworkCredential(Username, Password); 

     for (int i = 0; i <= directories.Count - 1; i++) 
     { 
      if (directories[i].Contains(".")) 
      { 

       string path = remoteFTPPath + @"/" + directories[i].ToString(); 
       string trnsfrpth = localFilesPath + @"\" + directories[i].ToString(); 

       ftpClient.DownloadFile(path, trnsfrpth); 
      } 
     } 
    } 

    response.Close(); 
} 

我收到的路径不支持例外,当我检查我的变量pathtrnsfrpth的价值观,他们似乎是包括Apache的信息。

路径:ftp://hostname/data/resourceOrders/-rw-r--r-- 1阿帕奇
阿帕奇367 7月16日14:07资源订单1437019656813-893.json

而且

trnsfrpth:五: \ code.runner \ local \ orders-rw-r - r-- 1 apache apache
367 Jul 16 14:07 resource-orders-1437019 656813-893.json

我如何能捕捉不仅是文件名,resource-orders-1437019656813-893.json没有哈克(rightof(),例如)的方法呢?

回答

3

这里是我使用的功能:

public class FileName : IComparable<FileName> 
{ 
    public string fName { get; set; } 
    public int CompareTo(FileName other) 
    { 
     return fName.CompareTo(other.fName); 
    } 
} 

public static void getFileList(string sourceURI, string sourceUser, string sourcePass, List<FileName> sourceFileList) 
{ 
    string line = ""; 
    FtpWebRequest sourceRequest; 
    sourceRequest = (FtpWebRequest)WebRequest.Create(sourceURI); 
    sourceRequest.Credentials = new NetworkCredential(sourceUser, sourcePass); 
    sourceRequest.Method = WebRequestMethods.Ftp.ListDirectory; 
    sourceRequest.UseBinary = true; 
    sourceRequest.KeepAlive = false; 
    sourceRequest.Timeout = -1; 
    sourceRequest.UsePassive = true; 
    FtpWebResponse sourceRespone = (FtpWebResponse)sourceRequest.GetResponse(); 
    //Creates a list(fileList) of the file names 
    using (Stream responseStream = sourceRespone.GetResponseStream()) 
    { 
     using (StreamReader reader = new StreamReader(responseStream)) 
     { 
      line = reader.ReadLine(); 
      while (line != null) 
      { 
       var fileName = new FileName 
         { 
          fName = line 
         }; 
         sourceFileList.Add(fileName); 
       line = reader.ReadLine(); 
      } 
     } 
    } 
} 

在main(设置你的sourceURI,用户名和密码),并宣布类似的文件列表:

List<FileName> sourceFileList = new List<FileName>(); 
string sourceURI = "ftp://www.sourceftp.com/"; 
string sourceUser = "testUser"; 
string sourcePass = "testPass"; 

这会给你一个轻松迭代文件名列表(sourceFileList [i] .fName)通过!这些可以使用.Sort()排序,您也可以执行二进制搜索。