SSH.Net SftpClient: do I need to call Disconnect within using block?

2.2k views Asked by At

In a code like this

using (var sftp = new SftpClient(_host, _userName, _password))
{
  sftp.Connect();
  // Do some work with sftp
  sftp.Disconnect();
}

Is sftp.Disconnect() call necessary or will using block (Dispose/Close/whatever) close connection automatically?

1

There are 1 answers

0
Dávid Szabó On

No, you don't need to call Disconnect within a using block.

SftpClient automatically calls the Disconnect method when disposing of the object.

See relevant source code:

https://github.com/sshnet/SSH.NET/blob/7bdfc9e615b736b2762f5cd83c31a5f669663ff6/src/Renci.SshNet/BaseClient.cs#L409-L428