简体   繁体   English

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

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

I'm trying to download files from a directory, without the other directories in the wanted directory. 我正在尝试从目录下载文件,而在通缉目录中没有其他目录。

I've searched over the Internet for an answer, and the only thing I found is to use FileMask "|*/" in TransferOptions , which isn't working, and downloads nothing. 我已经在Internet上搜索了答案,我发现的唯一发现是在TransferOptions使用FileMask "|*/" ,它不起作用,并且不下载任何内容。

Using the latest version (5.7.5) 使用最新版本(5.7.5)

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

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

Your code to exclude subdirectories is correct. 您排除子目录的代码是正确的。

See also WinSCP FAQ How do I transfer directory non-recursively? 另请参见WinSCP常见问题解答如何非递归传输目录?

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

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

It's an equivalent to your code. 这等效于您的代码。


You have other problems in your code. 您的代码中还有其他问题。

  1. You ask WinSCP to download the root directory, yet to exclude all directories. 您要求WinSCP下载根目录,但要排除所有目录。 So nothing is downloaded. 所以什么也没有下载。 You need to ask to download all files in the root directory: /* . 您需要要求下载根目录/*中的所有文件。

    Quoting the documentation for the remotePath parameter of the GetFiles : 引用文档获取GetFilesremotePath参数

    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 * . 要下载目录中的所有文件,请使用mask *

  2. I assume that the C:\\bla is a target directory. 我假设C:\\bla是目标目录。 So you have to tell WinSCP to download the files to the directory by appending a backslash: C:\\bla\\ . 因此,您必须告诉WinSCP通过添加反斜杠C:\\bla\\将文件下载到目录中。 Otherwise WinSCP will try to save all files to C:\\ with name bla . 否则,WinSCP将尝试将所有文​​件保存到名称为bla C:\\ Failing, if there's already a directory with that name. 失败,如果已经有一个具有该名称的目录。 Or overwriting the files one with another, if there's not. 或者,如果没有,则将文件彼此覆盖。

    Documentation for the localPath parameter of the GetFiles : GetFileslocalPath参数的文档:

    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. You should check for errors, either by inspecting the returned TransferOperationResult or by calling the .Check() directly. 您应该通过检查返回的TransferOperationResult或直接调用.Check()来检查错误。

So the correct code is: 因此正确的代码是:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM