【问题标题】:SSH.Net SftpClient: do I need to call Disconnect within using block?SSH.Net SftpClient:我需要在 using 块内调用 Disconnect 吗?
【发布时间】:2017-01-06 17:42:17
【问题描述】:

在这样的代码中

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

sftp.Disconnect() 调用是必要的还是会使用块(Dispose/Close/whatever)自动关闭连接?

【问题讨论】:

    标签: c# using idisposable disconnect ssh.net


    【解决方案1】:

    不,您不需要在 using 块内调用 Disconnect

    SftpClient 在处理对象时自动调用Disconnect 方法。

    查看相关源码:

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

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2013-06-08
      • 2019-12-26
      • 1970-01-01
      • 2018-07-19
      • 2010-12-07
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多