/// <summary> /// 得到子目录和路径中文件,在枚举中使用所有文件夹 /// 当运行FTP LIST 协议方法得到详细的文件列表。 /// 在一个FTP服务器上,服务器响应许多信息记录,每个记录代表一个文件 /// </summary> public IEnumerable <FTPFileSystem> GetSubDirectoriesAndFiles(Uri url) { FtpWebRequest request = WebRequest.Create(url) as FtpWebRequest; request.Credentials = this.Credentials; request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = null; Stream responseStream = null; StreamReader reader = null; try { response = request.GetResponse() as FtpWebResponse; this.OnNewMessageArrived(new NewMessageEventArg { NewMessage = response.StatusDescription }); responseStream = response.GetResponseStream(); reader = new StreamReader(responseStream); List <FTPFileSystem> subDirs = new List <FTPFileSystem>(); string subDir = reader.ReadLine(); //从记录字符串中找出FTP目录列表格式 FTPDirectoryListingStyle style = FTPDirectoryListingStyle.MSDOS; if (!string.IsNullOrEmpty(subDir)) { style = FTPFileSystem.GetDirectoryListingStyle(subDir); } while (!string.IsNullOrEmpty(subDir)) { subDirs.Add(FTPFileSystem.ParseRecordString(url, subDir, style)); subDir = reader.ReadLine(); } return(subDirs); } finally { if (response != null) { response.Close(); } //关闭StreamReader对象和相关流,释放reader系统资源 if (reader != null) { reader.Close(); } } }
/// <summary> /// Get the sub directories and files of the Url. It will be used in enumerate /// all the folders. /// When run the FTP LIST protocol method to get a detailed listing of the files /// on an FTP server, the server will response many records of information. Each /// record represents a file. /// </summary> public IEnumerable <FTPFileSystem> GetSubDirectoriesAndFiles(Uri url) { FtpWebRequest request = WebRequest.Create(url) as FtpWebRequest; request.Credentials = this.Credentials; request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = null; Stream responseStream = null; StreamReader reader = null; try { response = request.GetResponse() as FtpWebResponse; this.OnNewMessageArrived(new NewMessageEventArg { NewMessage = response.StatusDescription }); responseStream = response.GetResponseStream(); reader = new StreamReader(responseStream); List <FTPFileSystem> subDirs = new List <FTPFileSystem>(); string subDir = reader.ReadLine(); // Find out the FTP Directory Listing Style from the recordString. FTPDirectoryListingStyle style = FTPDirectoryListingStyle.MSDOS; if (!string.IsNullOrEmpty(subDir)) { style = FTPFileSystem.GetDirectoryListingStyle(subDir); } while (!string.IsNullOrEmpty(subDir)) { subDirs.Add(FTPFileSystem.ParseRecordString(url, subDir, style)); subDir = reader.ReadLine(); } return(subDirs); } finally { if (response != null) { response.Close(); } // Close the StreamReader object and the underlying stream, and release // any system resources associated with the reader. if (reader != null) { reader.Close(); } } }