zoukankan      html  css  js  c++  java
  • C# 使用ftp下载一个文件夹下的所有文件,包括子目录文件夹

      这篇博客给大家补充一个方法,就是得到一个目录下的所有文件名称。在前端调用,大家写一个递归去遍历就可以了,我在这里就不在写了。具体ftp下载的方法在我的另一篇博客里有,需要的可以去看一下。

    /// <summary>
            /// 读取文件目录下所有的文件名称,包括文件夹名称
            /// </summary>
            /// <param name="ftpAdd">传过来的文件夹路径</param>
            /// <returns>返回的文件或文件夹名称</returns>
            public static string[] GetFtpFileList(string ftpAdd )
            {
    
                string url = FTPCONSTR + ftpAdd;
                FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(url));
                ftpRequest.UseBinary = true;
                ftpRequest.Credentials = new NetworkCredential(FTPUSERNAME, FTPPASSWORD);
    
                if (ftpRequest != null)
                {
                    StringBuilder fileListBuilder = new StringBuilder();
                    //ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;//该方法可以得到文件名称的详细资源,包括修改时间、类型等这些属性
                    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;//只得到文件或文件夹的名称
                    try
                    {
    
                        WebResponse ftpResponse = ftpRequest.GetResponse();
                        StreamReader ftpFileListReader = new StreamReader(ftpResponse.GetResponseStream(), Encoding.Default);
    
                        string line = ftpFileListReader.ReadLine();
                        while (line != null)
                        {
                            fileListBuilder.Append(line);
                            fileListBuilder.Append("@");//每个文件名称之间用@符号隔开,便于前端调用的时候解析
                            line = ftpFileListReader.ReadLine();
                        }
                        ftpFileListReader.Close();
                        ftpResponse.Close();
                        fileListBuilder.Remove(fileListBuilder.ToString().LastIndexOf("@"), 1);
                        return fileListBuilder.ToString().Split('@');//返回得到的数组
                    }
                    catch (Exception ex)
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }

      FTP实现文件的下载功能请参考博客:http://www.cnblogs.com/zhenzaizai/p/7434669.html。

  • 相关阅读:
    mysql日常~gh-ost使用
    redis基础篇~哨兵
    zeppelin-0.6.0安装配置
    spark 好文链接
    spark API 介绍链接
    solr5.5 基于内置jetty配置 Ubuntu
    Gollum 安装笔记
    手机版测试
    win7 eclipse 调试storm
    (转)Storm UI 解释
  • 原文地址:https://www.cnblogs.com/zhenzaizai/p/8046116.html
Copyright © 2011-2022 走看看