• FTP文件操作之获取文件列表


    前面已经介绍了很多关于FTP对文件的操作,今天再跟大家介绍一个获取文件列表的功能。这个功能应该算是最简单的一个了,它只是获取了一下文件信息,而没有进行实质上的数据传输。

    下面是是该功能的核心代码:

    /// <summary>         /// 获取文件列表           /// </summary>         /// <param name="ftpServerIP">服务器地址</param>         /// <param name="ftpUserID">FTP用户名</param>         /// <param name="ftpPassword">FTP密码</param>         /// <returns></returns>         public string[] GetFileList(string ftpServerIP, string ftpUserID, string ftpPassword)         {             string[] downloadFiles;             StringBuilder result = new StringBuilder();             FtpWebRequest reqFTP;             try             {                 // 根据uri创建FtpWebRequest对象                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP));                 // 指定数据传输类型                   reqFTP.UseBinary = true;                 // ftp用户名和密码                   reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                 // 指定执行什么命令                   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("
    ");                     line = reader.ReadLine();                 }                 result.Remove(result.ToString().LastIndexOf('
    '), 1);                 //关闭流
                    reader.Close();                 response.Close();                 return result.ToString().Split('
    ');             }             catch (Exception ex)             {                 downloadFiles = null;                 return downloadFiles;             }         }

    关于FTP的文章写了好几篇了,C#的FTP基本操作写得都差不多了。这篇博客应该是最后一篇关于C#+FTP的文章了。

  • 相关阅读:
    poj 2478 Farey Sequence
    “玲珑杯”第七届郑州轻工业学院ACM程序设计大赛 ------- D:社交网络
    bnu oj 13288 Bi-shoe and Phi-shoe
    uva 11029 Leading and Trailing
    hdu 1198 Farm Irrigation
    hdu 4965 Fast Matrix Calculation
    Preparing Olympiad---cf550B(DFS或者状态压缩模板)
    squee_spoon and his Cube VI---郑大校赛(求最长子串)
    确定比赛名次---hdu1285(拓扑排序)
    Buy the souvenirs---hdu2126(01背包输出方案数)
  • 原文地址:https://www.cnblogs.com/liulaocai2/p/4337607.html
Copyright © 2020-2023  润新知