简体   繁体   English

使用WinSCP .NET程序集传输后比较文件

[英]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). 我正在使用WinSCP .NET程序集将文件从Windows传输到Unix服务器(主要是.doc文件)。 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. 我无法进行目录级同步,因为我正在将文档从其他客户端计算机传输到Unix服务器。 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. 在服务器上有适当的支持,您可以使用Session.CalculateFileChecksum来计算上载文件的校验和。 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#). 有关完整示例,请参阅通过SFTP / FTP协议将远程文件的校验和与本地文件进行校验 (在PowerShell中,但是将其轻松转换为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. 除非有特殊情况,否则不要使用.Abort()方法。 Also no point or using the .Dispose() , as you are at the end of the using block that calls the .Dispose() implicitly. 也没有意义或使用.Dispose() ,就像您在using块的.Dispose()隐式调用.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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