Пример #1
0
        void ListFiles(string path, out IList <string> directories, out IList <string> files)
        {
            directories = new List <string>();
            files       = new List <string>();

            // дикий костыль: сначала берём список имён
            // потом берём расширенный список и в нём ищем имена

            var unresolvedNames = new List <string>();
            {
                var request = GetRequest(path);
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                var response = (FtpWebResponse)request.GetResponse();
                var reader   = new StreamReader(response.GetResponseStream());
                var line     = reader.ReadLine();
                while (line != null)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        unresolvedNames.Add(line);
                    }
                    line = reader.ReadLine();
                }
            }

            var names = new List <string>();
            {
                var request = GetRequest(path);
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                var response = (FtpWebResponse)request.GetResponse();
                var reader   = new StreamReader(response.GetResponseStream());
                var line     = reader.ReadLine();
                while (line != null)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        names.Add(line);
                    }
                    line = reader.ReadLine();
                }
            }

            var data = FtpCrutch.MatchLists(unresolvedNames, names);

            foreach (var el in data)
            {
                if (el.Value.StartsWith("d", StringComparison.Ordinal))
                {
                    directories.Add(el.Key);
                }
                else
                if (el.Value.StartsWith("-", StringComparison.Ordinal))
                {
                    files.Add(el.Key);
                }
            }
        }
Пример #2
0
        void ListFiles(string path, out IList <string> directories, out IList <string> files)
        {
            directories = new List <string>();
            files       = new List <string>();

            // дикий костыль: сначала берём список имён
            // потом берём расширенный список и в нём ищем имена

            var unresolvedNames = new List <string>();
            {
                var request = GetRequest(path);
                request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                var response = (FtpWebResponse)request.GetResponse();
                var reader   = new StreamReader(response.GetResponseStream());
                var line     = reader.ReadLine();
                while (line != null)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        unresolvedNames.Add(line);
                    }
                    line = reader.ReadLine();
                }
            }

            var names = new List <string>();
            {
                var request = GetRequest(path);
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                var response = (FtpWebResponse)request.GetResponse();
                var reader   = new StreamReader(response.GetResponseStream());
                var line     = reader.ReadLine();
                while (line != null)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        names.Add(line);
                    }
                    line = reader.ReadLine();
                }
            }

            var data = FtpCrutch.MatchLists(unresolvedNames, names);

            FtpCrutch.SortData(data, ref files, ref directories);
        }
Пример #3
0
        public ArrayImpl FindFiles(string path, string mask = null, bool recursive = true)
        {
            var result = ArrayImpl.Constructor() as ArrayImpl;

            Regex maskChecker = null;

            if (!string.IsNullOrEmpty(mask))
            {
                maskChecker = FtpCrutch.GetRegexForFileMask(mask);
            }

            if (!string.IsNullOrEmpty(path) && !path.EndsWith("/", StringComparison.Ordinal))
            {
                path += "/";
            }
            path = ApplyCurrentPath(path);

            IList <string> files, directories;

            try
            {
                ListFiles(path, out directories, out files);
            }
            catch (System.Net.ProtocolViolationException)
            {
                return(result);
            }
            catch (WebException ex)
            {
                var error = (FtpWebResponse)ex.Response;
                if (error.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                {
                    return(result);
                }
                throw ex;
            }
            catch
            {
                throw;
            }

            foreach (var dirName in directories)
            {
                var dirEntry = new FtpFile(this, path, dirName, isDir: true);
                if (maskChecker?.IsMatch(dirName) ?? true)
                {
                    result.Add(dirEntry);
                }
                if (recursive)
                {
                    var filesInDir = FindFiles(dirEntry.FullName, mask, recursive);
                    foreach (var fileEntry in filesInDir)
                    {
                        result.Add(fileEntry);
                    }
                }
            }

            foreach (var fileName in files)
            {
                if (maskChecker?.IsMatch(fileName) ?? true)
                {
                    var fileEntry = new FtpFile(this, path, fileName);
                    result.Add(fileEntry);
                }
            }

            return(result);
        }