便于前端调用的时候解析 line = ftpFileListReader.ReadLine();}ftpFileList

时间:2021-12-30 07:02:28

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

/// <summary> /// 读取文件目录下所有的文件名称,包孕文件夹名称 /// </summary> /// <param>传过来的文件夹路径</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实现文件的下载成果请参考博客:。