問題描述
我正在嘗試從具有 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 模式獲取遠程目錄的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!