简体   繁体   English

使用WinSCP .NET程序集列出具有特定扩展名的文件

[英]List files with specific extension using WinSCP .NET assembly

I am using WinSCP .NET assembly to do a download and upload through SFTP with C# .NET. 我正在使用WinSCP .NET程序集通过带有C#.NET的SFTP进行下载和上传。 I have the download function working but I am looking for a way to have the files in the remote server listed (or at least listed with a specific extension) so user only have to choose from those files with the specific extension (like .txt ) to get the files they want. 我可以使用下载功能,但是我正在寻找一种方法来列出远程服务器中的文件(或至少以特定扩展名列出),因此用户只需从具有特定扩展名的文件中进行选择(例如.txt )。获得他们想要的文件。

Is there a way to do that with WinSCP .NET assembly? 有没有办法使用WinSCP .NET程序集来做到这一点?

Use the Session.ListDirectories method : 使用Session.ListDirectories方法

RemoteDirectoryInfo directory = session.ListDirectory("/home/martin");

foreach (RemoteFileInfo fileInfo in directory.Files)
{
    string extension = Path.GetExtension(fileInfo.Name);
    if (string.Compare(extension, ".txt", true) == 0)
    {
        Console.WriteLine("Adding {0} to listing", fileInfo.Name);
    }
}

Or the Session.EnumerateRemoteFiles method : Session.EnumerateRemoteFiles方法

IEnumerable<RemoteFileInfo> fileInfos =
    session.EnumerateRemoteFiles("/home/martin", "*.txt", EnumerationOptions.None);
foreach (RemoteFileInfo fileInfo in fileInfos)
{
    Console.WriteLine("Adding {0} to listing", fileInfo.Name);
}

Yes, you can use the RemoteFileInfo and RemoteDirectoryInfo classes to get remote file and directory details. 是的,您可以使用RemoteFileInfoRemoteDirectoryInfo类来获取远程文件和目录详细信息。 Then, depending on your requirements, you can use custom logic to retrieve specific files. 然后,根据您的要求,您可以使用自定义逻辑来检索特定文件。

More info on that here . 有关此的更多信息。

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

相关问题 使用 WinSCP .NET 程序集从 FTP 服务器下载除特定文件夹之外的所有文件 - Download all files except a specific folder from FTP server using WinSCP .NET assembly 使用WinSCP .NET程序集通过FTPS(安全)发送文件 - Sending files over FTPS (secure) using WinSCP .NET assembly 在 Linux 上使用 WinSCP .NET 组件 - Using WinSCP .NET assembly on Linux 使用 WinSCP .NET 程序集上传后删除文件 - Delete files after upload with WinSCP .NET assembly WinSCP .NET程序集-如何将文件上传到主目录? - WinSCP .NET assembly - How to upload files to home? 使用 WinSCP .NET 程序集检查 SFTP 服务器上是否存在任何具有扩展名的文件 - Check if any file with an extension exists on SFTP server using WinSCP .NET assembly 使用WinSCP .NET程序集仅使用SFTP协议删除或移动所选文件 - Remove or move only selected files with SFTP protocol using WinSCP .NET assembly SSH 主机密钥指纹...与模式不匹配...在 C# 中使用 WinSCP .NET 程序集下载文件时 - SSH host key fingerprint ... does not match pattern ... when using WinSCP .NET assembly in C# to download files WinSCP .NET 程序集 - 在 GetFiles 之后删除文件(不是目录) - WinSCP .NET assembly - Remove files (not directories) after GetFiles C# WinSCP .NET 程序集:如何异步上传多个文件 - C# WinSCP .NET assembly: How to upload multiple files asynchronously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM