コード例 #1
0
ファイル: DEFtp.cs プロジェクト: InfFelixNaumann/des
        }         // func ParseFtpDateTime

        /// <summary></summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <param name="searchPattern"></param>
        /// <param name="searchOption"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public IEnumerable <T> Enumerate <T>(string path = null, string searchPattern = null, SearchOption searchOption = SearchOption.TopDirectoryOnly, Encoding encoding = null)
            where T : FtpItem
        {
            var subFolders = new Stack <string>();

            subFolders.Push(path);

            var filterExpr         = Procs.GetFilerFunction(searchPattern, true);
            var attributeConverter = new FtpItemAtributesConverter();

            var isDirectoriesAllowed = typeof(T).IsAssignableFrom(typeof(FtpDirectory));
            var isFilesAllowed       = typeof(T).IsAssignableFrom(typeof(FtpFile));

            while (subFolders.Count > 0)
            {
                var currentPath = subFolders.Pop();

                using (var response = OpenRequest(WebRequestMethods.Ftp.ListDirectoryDetails, currentPath))
                    using (var sr = new StreamReader(response.GetResponseStream(), encoding ?? Encoding.UTF8))
                    {
                        int    pos;
                        string line;
                        while ((line = sr.ReadLine()) != null)
                        {
                            line = line.Trim();

                            // Rights (fix Länge 10 Zeichen)
                            var attributes = (FtpItemAttributes)attributeConverter.ConvertFromString(line.Substring(0, 10));

                            pos = 10;
                            ParseListColumn(line, ref pos);                     // Number of sub directories
                            var user  = ParseListColumn(line, ref pos);
                            var group = ParseListColumn(line, ref pos);

                            // Size
                            if (!Int64.TryParse(ParseListColumn(line, ref pos), out var size))
                            {
                                size = -1;
                            }

                            // Date z.B. yyyy-mm-dd hh:mm  oder mmm dd {yyyy | hh:mm}
                            var lastWrite = ParseFtpDateTime(line, ref pos);

                            // Filename
                            var name = line.Substring(pos).Trim();
                            if (name == "." || name == "..")
                            {
                                continue;
                            }

                            // return result
                            if ((attributes & FtpItemAttributes.Directory) != 0)
                            {
                                if (isDirectoriesAllowed && filterExpr(name))
                                {
                                    yield return((T)(object)new FtpDirectory(this, currentPath, name, lastWrite, attributes));
                                }

                                if (searchOption == SearchOption.AllDirectories)
                                {
                                    subFolders.Push(String.IsNullOrEmpty(currentPath) ? name : currentPath + '/' + name);
                                }
                            }
                            else if (isFilesAllowed && filterExpr(name))
                            {
                                yield return((T)(object)new FtpFile(this, currentPath, name, size, lastWrite, attributes));
                            }
                        }
                    }
            }
        }         // proc Enumerate