Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 519
vkelman Avatar asked Jan 06 '17 17:01

vkelman


1 Answers

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

like image 136
Dávid Szabó Avatar answered Oct 28 '22 05:10

Dávid Szabó