C# 遍历FTP文件夹/下载

本文介绍了一种使用C#实现FTP文件下载的方法,并详细解释了如何递归地遍历FTP目录以下载整个目录的内容。文章提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


整个程序大致可以分为2个部分,第一部分是实现单个文件下载的方法 

[c-sharp] view plain copy
  1. /// <summary>  
  2. /// 单个文件下载方法  
  3.  /// </summary>  
  4. /// <param name="adss">保存文件的本地路径</param>  
  5. /// <param name="ftpadss">下载文件的FTP路径</param>  
  6. public void download(string adss, string ftpadss)  
  7. {  
  8.     //FileMode常数确定如何打开或创建文件,指定操作系统应创建新文件。  
  9.     //FileMode.Create如果文件已存在,它将被改写  
  10.     FileStream outputStream = new FileStream(adss, FileMode.Create);  
  11.     FtpWebRequest downRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpadss));  
  12.     //设置要发送到 FTP 服务器的命令  
  13.     downRequest.Method = WebRequestMethods.Ftp.DownloadFile;  
  14.     FtpWebResponse response = (FtpWebResponse)downRequest.GetResponse();  
  15.     Stream ftpStream = response.GetResponseStream();  
  16.     long cl = response.ContentLength;  
  17.     int bufferSize = 2048;  
  18.     int readCount;  
  19.     byte[] buffer = new byte[bufferSize];  
  20.     readCount = ftpStream.Read(buffer, 0, bufferSize);  
  21.     while (readCount > 0)  
  22.     {  
  23.         outputStream.Write(buffer, 0, readCount);  
  24.         readCount = ftpStream.Read(buffer, 0, bufferSize);  
  25.     }  
  26.     ftpStream.Close();  
  27.     outputStream.Close();  
  28.     response.Close();  
  29. }   

  第二个部分也就是需要遍历出我们所指定的文件夹内所有内容 

  首先是一个单个遍历文件夹获取文件夹下所有文件信息的方法  

[c-sharp] view plain copy
  1. /// </summary>  
  2. /// <param name="ftpads">FTP地址路径</param>  
  3. /// <param name="name">我们所选择的文件或者文件夹名字</param>  
  4. /// <param name="type">要发送到FTP服务器的命令</param>  
  5. /// <returns></returns>  
  6. public string[] ftp(string ftpads,string name,string type)  
  7. {  
  8.     WebResponse webresp = null;  
  9.     StreamReader ftpFileListReader = null;  
  10.     FtpWebRequest ftpRequest=null;  
  11.     try  
  12.     {  
  13.          ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(ftpads + name));  
  14.          ftpRequest.Method = type;  
  15.          webresp = ftpRequest.GetResponse();  
  16.          ftpFileListReader = new StreamReader(webresp.GetResponseStream(), Encoding.Default);  
  17.     }  
  18.     catch(Exception ex)  
  19.     {  
  20.         ex.ToString();  
  21.           
  22.     }  
  23.     StringBuilder str = new StringBuilder();  
  24.     string line=ftpFileListReader.ReadLine();  
  25.     while (line != null)  
  26.     {  
  27.         str.Append(line);  
  28.         str.Append("/n");  
  29.         line = ftpFileListReader.ReadLine();  
  30.     }  
  31.     string[] fen = str.ToString().Split('/n');  
  32.     return fen;  
  33. }  

  之后是一个我们实现递归文件夹的方法 

[c-sharp] view plain copy
  1. /// <summary>  
  2. /// 下载方法KO  
  3. /// </summary>  
  4. /// <param name="ftpads">FTP路径</param>  
  5. /// <param name="name">需要下载文件路径</param>  
  6. /// <param name="Myads">保存的本地路径</param>  
  7. public void downftp(string ftpads, string name,string Myads)  
  8. {  
  9.     string downloadDir = Myads + name;  
  10.     string ftpdir = ftpads + name;  
  11.     string[] fullname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectoryDetails);  
  12.     //判断是否为单个文件   
  13.     if (fullname.Length <= 2)  
  14.     {  
  15.         if (fullname[fullname.Length - 1] == "")  
  16.         {  
  17.             download(downloadDir + "/" + name, ftpads + name + "/" + name);  
  18.         }  
  19.     }  
  20.     else  
  21.     {  
  22.         string[] onlyname = ftp(ftpads, name, WebRequestMethods.Ftp.ListDirectory);  
  23.         if (!Directory.Exists(downloadDir))  
  24.         {  
  25.             Directory.CreateDirectory(downloadDir);  
  26.         }  
  27.         foreach (string names in fullname)  
  28.         {  
  29.             //判断是否具有文件夹标识<DIR>  
  30.             if (names.Contains("<DIR>"))  
  31.             {  
  32.                 string olname = names.Split(new string[] { "<DIR>" },   
  33.                 StringSplitOptions.None)[1].Trim();  
  34.                 downftp(ftpdir, "//" + olname, downloadDir);  
  35.             }  
  36.             else  
  37.             {  
  38.                 foreach (string onlynames in onlyname)  
  39.                 {  
  40.                     if (onlynames == "" || onlynames == " " || names == "")  
  41.                     {  
  42.                         break;  
  43.                     }  
  44.                     else  
  45.                     {  
  46.                         if (names.Contains(" " + onlynames))  
  47.                         {  
  48.                             download(downloadDir + "/" + onlynames, ftpads + name + "/" +   
  49.                             onlynames);  
  50.                             break;  
  51.                         }  
  52.                     }  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57.       
  58. }  

      在使用WebRequestMethods.Ftp.ListDirectoryDetails取得文件夹下所有内容时,会发现如果其中有文件夹,那么文件夹的的详细信息中会有一个"<DIR>"标识,我们就可以通过这个来将其区分开来

      同时在获取文件夹以及文件名称时用到WebRequestMethods.Ftp.ListDirectory,这个指令能过只获得文件夹下所有文件包括文件夹的名字,通过这两个指令所获取的信息逐一比较,便能确定出文件或文件夹名以传递到download和downftp方法中


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值