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 模式获取远程目录的文件


- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04