コード例 #1
0
        public List <FtpDirectoryEntry> ListDirectory(string dir, bool recursive = false)
        {
            var retVal = new List <FtpDirectoryEntry>();

            var request = GetNewRequest(Hostname.Concat(dir), WebRequestMethods.Ftp.ListDirectoryDetails);

            using (var response = request.GetResponse())
            {
                using (var stream = new StreamReader(response.GetResponseStream()))
                {
                    while (!stream.EndOfStream)
                    {
                        var line  = stream.ReadLine();
                        var entry = FtpDirectoryEntry.Parse(dir, line);
                        retVal.Add(entry);
                    }
                }
            }

            if (recursive)
            {
                var add = new List <FtpDirectoryEntry>();
                foreach (var d in retVal.Where(e => e.IsDirectory))
                {
                    var list = ListDirectory(d.Name, recursive);
                    add.AddRange(list);
                }
                retVal.AddRange(add);
            }

            return(retVal);
        }
コード例 #2
0
        public bool UploadDirectory(string directory, string destination)
        {
            if (directory[directory.Length - 1] != Path.DirectorySeparatorChar)
            {
                directory += Path.DirectorySeparatorChar;
            }

            var fileList = FileList.Build(new Configuration.SourceDirectory()
            {
                Directory = directory
            });
            var logger = Logger.Global;

            Parallel.ForEach(fileList.Files,
                             new ParallelOptions()
            {
                MaxDegreeOfParallelism = Threads
            },
                             f =>
            {
                var relPath    = directory.RelativePath(f.Path).Replace('\\', '/');
                var relPathDir = directory.RelativePath(Path.GetDirectoryName(f.Path)).Replace('\\', '/');

                CreateDirectory(FtpDirectoryEntry.Combine(destination, relPathDir), true);

                logger.Write($"Uploading file {relPath}.");
                UploadFile(f.Path, FtpDirectoryEntry.Combine(destination, relPath));
            });

            return(false);
        }