首页 > 解决方案 > fluentftp上传目录跳过子文件夹

问题描述

我使用 fluentftp 库将文件夹上传到 ftp。我如何跳过目录的子文件夹。

              // upload only PDF files
            var rules = new List<FtpRule>{
               new FtpFileExtensionRule(true, new List<string>{ "pdf" }),
               new FtpFolderNameRule(false, FtpFolderNameRule.CommonBlacklistedFolders)
               // only allow PDF files
            };
            ftp.UploadDirectory(ServiceAbrechnungPath, @"/Abrechnungen",
                FtpFolderSyncMode.Mirror, FtpRemoteExists.Skip, FtpVerify.None, rules);

标签: c#fluentftp

解决方案


您需要添加FtpFolderNameRule以排除子文件夹。

使用您的代码,它看起来像这样;

using System.Linq

//Get a list of subfolders in the root folder without their path name.
//This should be just the folders in the root folder i.e. you don't need a recursive list of folders within these folders    
var subfolders = Directory.GetDirectories(ServiceAbrechnungPath).Select(subDirectory => subDirectory.Remove(0, ServiceAbrechnungPath.Length)).ToList();

// upload only PDF files in the root of ServiceAbrechnungPath
var rules = new List<FtpRule>{
    new FtpFileExtensionRule(true, new List<string>{ "pdf" }), // only allow PDF files
    new FtpFolderNameRule(false, subfolders)  // exclude subfolders    
};

var uploadResult = ftp.UploadDirectory(ServiceAbrechnungPath, @"/Abrechnungen", FtpFolderSyncMode.Mirror, FtpRemoteExists.Skip,FtpVerify.None, rules);

uploadResult变量将包含一个List<FtpResult>显示哪些文件已成功上传以及哪些文件夹/文件被规则跳过。


推荐阅读