Example #1
0
        /// <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.UseBinary   = true;
            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);
                reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);

                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();
                }
            }
        }
Example #2
0
        /// <summary>
        /// 获得Url的子目录和文件. 对于所有的文件夹他将使用枚举.

        /// 在一个FTP 服务上,当运行FTP LIST 协议方法来获得一个文件的详细列表时,
        /// 这个服务将响应一些信息的记录。每一个记录代表一个文件。
        /// </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();
                }
            }
        }