Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Calculating checksum match after transfer

Do your checksum test after GetFiles finishes. Not within FileTransferred event.

Though note what few servers actually support checksum calculation.

Also, you should call session.Check.
https://winscp.net/eng/docs/library_session#results
carenrose

Calculating checksum match after transfer

I'm attempting to get files from the remote server, then check that the checksum of the remote and local file match, either before deleting the remote file or just to ensure it was transferred properly.

using (var session = new Session())
{
    session.FileTransferred += Session_FileDownloaded;
    session.Open(new SessionOptions { ... });
 
    var files = session.EnumerateRemoteFiles(path, "", EnumerationOptions.None);
    foreach (var file in files)
    {
        session.GetFiles(file.FullName, Path.Combine(dest, file.Name));
    }
}


private static SHA1 sha1 = SHA1.Create();
 
private static void Session_FileDownloaded(object sender, TransferEventArgs e)
{
    var session = sender as Session;
    if (session != null)
    {
        var remote = e.FileName;
        var local  = e.Destination;
 
        var remoteChkSum =
            BitConverter.ToString(session.CalculateFileChecksum("sha-1", remote));
        var localChkSum  =
            BitConverter.ToString(sha1.ComputeHash(File.OpenRead(local)));
    }
}


It's throwing an InvalidOperationException on this line: BitConverter.ToString(session.CalculateFileChecksum("sha-1", remote)), with the message "Recursive calls not allowed".

What is the correct way to do this?