JDo
JDo

Reputation: 358

How to check file download SSH.NET C#

I'm using SSH.NET library in a desktop application. How can I arrange to check the upload to the server if the connection is broken. I conducted a small experiment with the folder synchronization method, and when the connection is broken, it creates a file on SFTP server, and when the network appears, it starts downloading.

public void SynchronizeDirectories(string sourceDirectory, string sourceFolder)
    {
        try
        {
            ConnectToHost();
            var searchPattern = "*";
            var upLoadedFiles =
                sftpClient.SynchronizeDirectories(sourceFolder, sourceDirectory, searchPattern);

            foreach (var file in upLoadedFiles)
            {
                LoggerManager.InfoMessage($"File on sftp has been updated {file.Name}");
            }
        }
        catch (Exception e)
        {
            LoggerManager.ErrorMessage("Error sftp server sync", e);
        }
        finally
        {
            Close();
        }
    }

Maybe is there way to keep the file from being created until a full sync occurs?? Or WinScp style with creating file with .filepath extencion.

Upvotes: 0

Views: 884

Answers (2)

JDo
JDo

Reputation: 358

Finally I created pull request to SSH.NET several days ago – Added filepart (.tmp) extension #791

I changed code in SftpClient.cs InternalSynchronizeDirectories method I added .tmp extension if file has not yet been uploaded to the server. Then extension is changed to the original when it was uploaded.

Upvotes: 1

Martin Prikryl
Martin Prikryl

Reputation: 202232

I'm not sure I understand what you want to change about behaviour of the SftpClient.SynchronizeDirectories. In any case, the method offers no customization whatsoever. But you can reuse its code and modify it the way you like. SSH.NET is open source.

Or use WinSCP .NET assembly and its Session.SynchronizeDirectories, if you like how WinSCP implements the synchronization.

Upvotes: 1

Related Questions