glcui 发表于 2015-11-6 10:58:40

C# 遍历FTP文件夹/下载

  原文链接:http://blog.iyunv.com/ou8811/article/details/5295780

  

  整个程序大致可以分为2个部分,第一部分是实现单个文件下载的方法
view plaincopy
[*]/// <summary>
[*]/// 单个文件下载方法
[*] /// </summary>
[*]/// <param name=&quot;adss&quot;>保存文件的本地路径</param>
[*]/// <param name=&quot;ftpadss&quot;>下载文件的FTP路径</param>
[*]public void download(string adss, string ftpadss)
[*]{
[*]    //FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。
[*]    //FileMode.Create如果文件已存在,它将被改写
[*]    FileStream outputStream = new FileStream(adss, FileMode.Create);
[*]    FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));
[*]    //设置要发送到 FTP 服务器的命令
[*]    downRequest.Method = WebRequestMethods.Ftp.DownloadFile;
[*]    FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();
[*]    Stream ftpStream = response.GetResponseStream();
[*]    long cl = response.ContentLength;
[*]    int bufferSize = 2048;
[*]    int readCount;
[*]    byte[] buffer = new byte;
[*]    readCount = ftpStream.Read(buffer, 0, bufferSize);
[*]    while (readCount > 0)
[*]    {
[*]      outputStream.Write(buffer, 0, readCount);
[*]      readCount = ftpStream.Read(buffer, 0, bufferSize);
[*]    }
[*]    ftpStream.Close();
[*]    outputStream.Close();
[*]    response.Close();
[*]}   
  第二个部分也就是需要遍历出我们所指定的文件夹内所有内容
  首先是一个单个遍历文件夹获取文件夹下所有文件信息的方法
view plaincopy
[*]/// </summary>
[*]/// <param name=&quot;ftpads&quot;>FTP地址路径</param>
[*]/// <param name=&quot;name&quot;>我们所选择的文件或者文件夹名字</param>
[*]/// <param name=&quot;type&quot;>要发送到FTP服务器的命令</param>
[*]/// <returns></returns>
[*]public string[] ftp(string ftpads,string name,string type)
[*]{
[*]    WebResponse webresp = null;
[*]    StreamReader ftpFileListReader = null;
[*]    FtpWebRequest ftpRequest=null;
[*]    try
[*]    {
[*]         ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpads &#43; name));
[*]         ftpRequest.Method = type;
[*]         webresp = ftpRequest.GetResponse();
[*]         ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);
[*]    }
[*]    catch(Exception ex)
[*]    {
[*]      ex.ToString();
[*]         
[*]    }
[*]    StringBuilder str = new StringBuilder();
[*]    string line=ftpFileListReader.ReadLine();
[*]    while (line != null)
[*]    {
[*]      str.Append(line);
[*]      str.Append(&quot;/n&quot;);
[*]      line = ftpFileListReader.ReadLine();
[*]    }
[*]    string[] fen = str.ToString().Split('/n');
[*]    return fen;
[*]}
  之后是一个我们实现递归文件夹的方法
view plaincopy
[*]/// <summary>
[*]/// 下载方法KO
[*]/// </summary>
[*]/// <param name=&quot;ftpads&quot;>FTP路径</param>
[*]/// <param name=&quot;name&quot;>需要下载文件路径</param>
[*]/// <param name=&quot;Myads&quot;>保存的本地路径</param>
[*]public void downftp(string ftpads, string name,string Myads)
[*]{
[*]    string downloadDir = Myads &#43; name;
[*]    string ftpdir = ftpads &#43; name;
[*]    string[] fullname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectoryDetails);
[*]    //判断是否为单个文件   
[*]    if (fullname.Length <= 2)
[*]    {
[*]      if (fullname == &quot;&quot;)
[*]      {
[*]            download(downloadDir &#43; &quot;/&quot; &#43; name, ftpads &#43; name &#43; &quot;/&quot; &#43; name);
[*]      }
[*]    }
[*]    else
[*]    {
[*]      string[] onlyname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectory);
[*]      if (!Directory.Exists(downloadDir))
[*]      {
[*]            Directory.CreateDirectory(downloadDir);
[*]      }
[*]      foreach (string names in fullname)
[*]      {
[*]            //判断是否具有文件夹标识<DIR>
[*]            if (names.Contains(&quot;<DIR>&quot;))
[*]            {
[*]                string olname = names.Split(new string[] { &quot;<DIR>&quot; },   
[*]                StringSplitOptions.None).Trim();
[*]                downftp(ftpdir, &quot;//&quot; &#43; olname, downloadDir);
[*]            }
[*]            else
[*]            {
[*]                foreach (string onlynames in onlyname)
[*]                {
[*]                  if (onlynames == &quot;&quot; || onlynames == &quot; &quot; || names == &quot;&quot;)
[*]                  {
[*]                        break;
[*]                  }
[*]                  else
[*]                  {
[*]                        if (names.Contains(&quot; &quot; &#43; onlynames))
[*]                        {
[*]                            download(downloadDir &#43; &quot;/&quot; &#43; onlynames, ftpads &#43; name &#43; &quot;/&quot; &#43;   
[*]                            onlynames);
[*]                            break;
[*]                        }
[*]                  }
[*]                }
[*]            }
[*]      }
[*]    }
[*]      
[*]}
  在使用WebRequestMethods.Ftp.ListDirectoryDetails取得文件夹下所有内容时,会发现如果其中有文件夹,那么文件夹的的详细信息中会有一个&quot;<DIR>&quot;标识,我们就可以通过这个来将其区分开来
  同时在获取文件夹以及文件名称时用到WebRequestMethods.Ftp.ListDirectory,这个指令能过只获得文件夹下所有文件包括文件夹的名字,通过这两个指令所获取的信息逐一比较,便能确定出文件或文件夹名以传递到download和downftp方法中
页: [1]
查看完整版本: C# 遍历FTP文件夹/下载