Automatically resume SFTP download after network connection is restored with SSH.NET

Viewed 3864

I am using SSH.NET library to create SFTP client. I need to resume the download, if network connection becomes available again within this timeout. I am using the below mentioned approach as shown in many examples.

PrivateKeyFile ObjPrivateKey = new PrivateKeyFile(keyStream);
PrivateKeyAuthenticationMethod ObjPrivateKeyAutentication = new PrivateKeyAuthenticationMethod(username, ObjPrivateKey);

var connectionInfo = new ConnectionInfo(hostAddress, port, username, ObjPrivateKeyAutentication);

try
{
    using (var client = new SftpClient(connectionInfo))
    {
        client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(10);

        client.Connect();

        if (!client.IsConnected)
        {
            return false;
        }

        if (!client.Exists(source))
        {
            return false;
        }

        var fileName = Path.GetFileName(source);

        using (var fs = new FileStream(destination + fileName, FileMode.Create))
        {
            client.DownloadFile(source, fs, printActionDel);
            fs.Close();
            returnState = true;
        }

        client.Disconnect();
        client.Dispose();
    }
}

I am unplugging the network cable to interrupt the download and test the timeout scenario. Though I enable the internet connection again within the timeout to resume the download, it is not resuming. What am I doing wrong here? Please advice.

1 Answers
Related