List files with specific extension using WinSCP .NET assembly

核能气质少年 提交于 2019-12-06 04:41:49

Use the Session.ListDirectories method:

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:

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. Then, depending on your requirements, you can use custom logic to retrieve specific files.

More info on that here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!