WinSCP .NET 程序集 - GetFiles 根目录非递归(无子目录)

WinSCP .NET assembly - GetFiles root directory non-recursive (without subdirs)

我正在尝试从目录下载文件,而所需目录中没有其他目录。

我在 Internet 上搜索了答案,我唯一找到的是在 TransferOptions 中使用 FileMask "|*/",这是行不通的,然后下载没有什么。

使用最新版本 (5.7.5)

TransferOptions t = new TransferOptions { FileMask = "|*/" };

session.GetFiles("/", @"C:\bla", false, t);

您排除子目录的代码是正确的。

另请参阅 WinSCP 常见问题解答 How do I transfer directory non-recursively?

TransferOptions transferOptions = new TransferOptions();
transferOptions.FileMask = "|*/";

session.PutFiles(@"d:\toupload\*", "/home/user/", false, transferOptions);

它等同于您的代码。


您的代码中还有其他问题。

  1. 你让WinSCP下载根目录,但排除了所有目录。所以没有下载任何东西。需要询问下载根目录下的所有文件:/*.

    引用 remotePath parameter of the GetFiles 的文档:

    Full path to remote directory followed by slash and wildcard to select files or subdirectories to download. To download all files in a directory, use mask *.

  2. 我假设 C:\bla 是一个目标目录。因此,您必须通过附加反斜杠来告诉 WinSCP 将文件下载到目录:C:\bla\。否则 WinSCP 将尝试将所有文​​件保存到 C:\,名称为 bla。失败,如果已经有同名的目录。或者用另一个覆盖文件,如果没有的话。

    localPath parameter of the GetFiles 的文档:

    Full path to download the file to. When downloading multiple files, the filename in the path should be replaced with operation mask or omitted (path ends with backslash).

  3. 您应该检查错误,方法是检查返回的 TransferOperationResult 或直接调用 .Check()

所以正确的代码是:

TransferOptions t = new TransferOptions { FileMask = "|*/" };

session.GetFiles("/*", @"C:\bla\", false, t).Check();