[英]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. 是的,您可以使用
RemoteFileInfo
和RemoteDirectoryInfo
类来获取远程文件和目录详细信息。 Then, depending on your requirements, you can use custom logic to retrieve specific files. 然后,根据您的要求,您可以使用自定义逻辑来检索特定文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.