Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

Is there a way to do that with WinSCP .NET assembly?

like image 646
Mahsa Avatar asked Oct 17 '25 01:10

Mahsa


2 Answers

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);
}
like image 171
Martin Prikryl Avatar answered Oct 18 '25 14:10

Martin Prikryl


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.

like image 28
shree.pat18 Avatar answered Oct 18 '25 14:10

shree.pat18



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!