简体   繁体   中英

Compare file after transfer using WinSCP .NET assembly

I am using WinSCP .NET assembly to transfer files from Windows to Unix server (mostly .doc files). Sometimes the file is transferred as blank document. But the source has come content in it. I can't go for directory level synchronization because am transferring documents to Unix server from different client machines. I am using the following code:

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = cls_appvars.Set_FTP_Host,
    UserName = cls_appvars.Set_FTP_User,
    Password = cls_appvars.Set_FTP_Password,
};

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

    TransferOptions transferOptions = new TransferOptions();
    transferOptions.TransferMode = TransferMode.Binary;

    TransferOperationResult transferResult;

    transferResult = session.GetFiles(wordfilepath, downloadwordpath + ".tmp", false, transferOptions);

    System.IO.File.Move(downloadwordpath + ".tmp", downloadwordpath);

    transferResult.Check();   

    foreach (TransferEventArgs transfer in transferResult.Transfers)
    {
        System.IO.File.AppendAllText(path, System.DateTime.Now + "***func_download_file_individual() in scribeapp*** Download succeeded for file " + transfer.FileName + Environment.NewLine);
    }

    session.Abort();
    session.Dispose();
}

Is there any way to check on the transferred file is synchronized with the source file?

With an appropriate support on the server, you can use the Session.CalculateFileChecksum to calculate checksum of the uploaded file. Compare that to a checksum of the source file to verify the upload was successful.

For a complete example see Verify checksum of a remote file against a local file over SFTP/FTP protocol (it's in PowerShell, but it should be easy to translate it to C#).

Though a correct solution would be to find out, why is the upload is failing in the first place.


Do not use the .Abort() method, unless in an exceptional situation. Also no point or using the .Dispose() , as you are at the end of the using block that calls the .Dispose() implicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM