无法为WinSCP会话类创建实例

时间:2013-06-26 15:08:25

标签: c#-4.0 sftp winscp winscp-net

我正在使用WinSCP下载和上传C#程序中的文件。我的代码看起来像这样

    public Session OpenSftpSession ()
    {
       SessionOptions sessionOptions = new SessionOptions ();
       sessionOptions.Protocol = WinSCP.Protocol.Sftp;
       sessionOptions.HostName = this.hostName_;
       sessionOptions.UserName = this.userName_;
       sessionOptions.Password = this.password_;
       sessionOptions.SshHostKeyFingerprint = this.sshHostKeyFingerprint_;

       Session session = null;
       try
       {
           session = new Session ();
           session.Open (sessionOptions);
       }
       catch (Exception ex)
       {
           return null;
       }

       return session;
    }

当程序执行行

    session = new Session ();

它终止了。我甚至没有任何例外,它只是终止。

谢谢, 达山。

3 个答案:

答案 0 :(得分:0)

即使遵循zmx的样本,我也遇到了同样的问题。它只是拒绝实例化Session对象。我最终做了以下(来自instructions on the WinSCP.net site):

  1. 下载5.2.4 beta版(.NET程序集和可移植可执行文件)。
  2. 添加对 WinSCPnet.dll 的引用(注意名称)。
  3. WinSCP.exe 放置在项目的根文件夹中。
  4. 使用“”的“构建操作”设置其属性(WinSCP.exe),以及“复制更新”的“复制到输出目录”
  5. DLL和EXE的组合使其按预期工作。只是使用DLL不起作用,因为它只是提供了一个围绕EXE的包装器。此外,旧版本的WinSCP程序集的名称为WinSCP.dll,与EXE的名称冲突;所以在bin文件夹中同时拥有DLL和EXE都不起作用。

答案 1 :(得分:0)

只需将WinScp.exe和winScp.dll粘贴到您的应用程序文件夹中,就像我一样:

ages_train, ages_test, net_worths_train, net_worths_test = train_test_split(ages, net_worths, test_size=0.1, random_state=42)

答案 2 :(得分:-1)

尝试像WinSCP website上的示例一样加载sessionOptions

例如,您的代码类似于:

 public Session OpenSftpSession ()
{
   SessionOptions sessionOptions = new SessionOptions
   { 
       Protocol = WinSCP.Protocol.Sftp;
       HostName = this.hostName_;
       UserName = this.userName_;
       Password = this.password_;
       SshHostKeyFingerprint = this.sshHostKeyFingerprint_;
   }

   Session session = null;
   try
   {
       session = new Session ();
       session.Open(sessionOptions);
   }
   catch (Exception ex)
   {
       return null;
   }

   return session;
}