问题描述
我正在尝试从具有 FTP/SFTP 连接的远程服务器获取特定文件,我遇到的问题是,我正在尝试获取具有特定模式的远程目录中的文件数.我正在使用面具,但对我不起作用,它会引发异常:这就是我所拥有的
I'm trying to get an specific files from a remote server with FTP/SFTP connection, the issue that I'm having is, I'm trying to get the count of files in the remote directory with an specific pattern. I'm using a mask but is not working for me, it throwing an exception: this is what I have
DataFile.sRemoteDirectory = "/user/ftpuser/test/";
receivepattern = "Del*";
filesCount =
session.ListDirectory(
session.EscapeFileMask(DataFile.sRemoteDirectory + receivepattern))
.Files.Where(x => !x.IsDirectory).Count();
推荐答案
Session.ListDirectory
方法 不接受通配符,只接受路径.
The Session.ListDirectory
method does not accept a wildcard, only a path.
由于 WinSCP .NET 程序集 5.9,您可以使用 Session.EnumerateRemoteFiles代码>方法改为:
Since, the WinSCP .NET assembly 5.9, you can use the Session.EnumerateRemoteFiles
method instead:
filesCount =
session.EnumerateRemoteFiles(
DataFile.sRemoteDirectory, receivepattern, EnumerationOptions.None).Count();
<小时>
在旧版本中,您必须自己过滤 Session.ListDirectory
返回的文件:
Regex r = new Regex("^Del.*");
filesCount = session.ListDirectory(DataFile.sRemoteDirectory).Files
.Where(x => !x.IsDirectory)
.Where(x => r.Match(x.Name))
.Count()
查看官方示例 列出与通配符匹配的文件(不过在 PowerShell 中).
See the official example Listing files matching wildcard (in PowerShell though).
这篇关于如何使用 C# 和 WinSCP 模式获取远程目录的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!