How to get the files of remote directory using a pattern with C# and WinSCP(如何使用 C# 和 WinSCP 模式获取远程目录的文件)
问题描述
我正在尝试从具有 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 模式获取远程目录的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C# 和 WinSCP 模式获取远程目录的文件
基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
