-1

I'm trying to implement some logic to compare file information between remote server and local server. I need to compare the file name between local folder and remote folder and download only the new files. I tried using loading files in a list and use Except function, it didn't work. Appreciate your help.

Please find one of the scenario I tried.

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    const string remotePath = "/Test";
    const string localPath = @"C:\Local";
    const string ArchivePath = @"C:\Users\Local\Archive";

    System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(ArchivePath);
    RemoteDirectoryInfo dir1 = session.ListDirectory(remotePath);

    IEnumerable<System.IO.FileInfo> list2 =
        dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
    IEnumerable<RemoteFileInfo> list1 =
        session.EnumerateRemoteFiles(remotePath, "*.csv", EnumerationOptions.None);

    var firstNotSecond = list1.Except(list2).ToList();
}

Getting error like

'IEnumerable' does not contain a definition for 'Except' and the best extension method overload 'Queryable.Except(IQueryable, IEnumerable)' requires a receiver of type 'IQueryable'

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Nitz
  • 21
  • 1

1 Answers1

1

You will have to compare just the filenames:

var firstNotSecond =
    list1.Select(_ => _.Name).Except(list2.Select(_ => _.Name)).ToList();

Though note that WinSCP .NET has this functionality built-in. There's Session.CompareDirectories.

And if you actually want to synchronize the directories, there's Session.SynchronizeDirectories. One method that will do everything for you.

session.SynchronizeDirectories(
    SynchronizationMode.Local, localPath, remotePath, false).Check()
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992