11

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?

Progman
  • 16,827
  • 6
  • 33
  • 48
vkelman
  • 1,501
  • 1
  • 15
  • 25

1 Answers1

3

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

Dávid Szabó
  • 2,235
  • 2
  • 14
  • 26