使用 SSH.NET 将文件上传到 SFTP:无法连接

Uploading file to SFTP using SSH.NET: Cannot connect

我构建了一个控制台应用程序来将文件上传到 FTP 服务器。由于此 FTP 服务器仅接受 SFTP 协议,我正在使用 SSH.NET 开源库,因为 .NET 中没有内置方法(.NET 3.5 的 less,这是一个我在 Visual Studio 2008 Professional 下使用。

我见过很多例子。我已经实现了一个快速测试应用程序来连接到 public SFTP 服务器并使用 SFTP 协议上传文件。

我正在使用 public 从 here:

获得的免费 SFTP 服务器
server   : demo.wftpserver.com
user     : demo-user
password : demo-user
port     : 2222

这是代码:

    const string host             = "demo.wftpserver.com";
    const string username         = "demo-user";
    const string password         = "demo-user";
    const string workingdirectory = "/upload";
    const string uploadfile       = @"C:\test.txt";
    const int    port             = 2222;

    static void Main(string[] args)
    {
        try
        {                
            using (SftpClient client = new SftpClient(host, port, username, password))
            {
                {
                    client.Connect();
                    Console.WriteLine("Connected to {0}", host);

                    client.ChangeDirectory(workingdirectory);
                    Console.WriteLine("Changed directory to {0}", workingdirectory);

                    var listDirectory = client.ListDirectory(workingdirectory, null);
                    Console.WriteLine("Listing directory:");
                    foreach (var fi in listDirectory)
                    {
                        Console.WriteLine(" - " + fi.Name);
                    }

                    using (var fileStream = new FileStream(uploadfile, FileMode.Open))
                    {
                        Console.WriteLine("Uploading {0} ({1:N0} bytes)", uploadfile, fileStream.Length);
                        client.BufferSize = 4 * 1024; // bypass Payload error large files
                        client.UploadFile(fileStream, Path.GetFileName(uploadfile), null);
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.Out.WriteLine(e.Message);
        }

问题如下:

尝试使用以下句子连接服务器时:

client.Connect();

我得到一个例外。异常说明如下:

"Value cannot be null. \r\nParameter name: All lists are either null or empty."

StackTrace  "   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)\r\n   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)\r\n   at Renci.SshNet.Session.Connect()\r\n   at Renci.SshNet.BaseClient.Connect()\r\n   at ConsoleApplication2.Program.Main(String[] args) at C:\Users\Win7\Downloads\ConsoleApplication2\ConsoleApplication2\Program.cs:line 61"    string

此外,我已尝试按照说明更改超时 here 但没有成功。

但是,如果我尝试使用来自 FileZilla 客户端的相同凭据连接到同一个 SFTP 服务器,那么我可以毫无问题地连接甚至上传文件。

所以我不明白发生了什么。有人可以帮助我吗?

更新: 我找到了这个 fix。该补丁于 2017 年 1 月 22 日在开发分支中应用,但最后一次下载二进制文件是 2016 年 12 月 14 日。那么我如何才能获得包含此修复程序的二进制文件?

我使用 Tamir.SharpSSH 在我们的 SFTP 中发送文件。尝试使用这个,下面的代码是将文件发送到您的 sftp 的最简单方法。

Sftp sftpBase = new Tamir.SharpSsh.Sftp(_sftpHost, _sftpUid, Up_decryptPass);
sftpBase.Connect(int.Parse(_sftpPort));
sftpBase.Put(Dir + filename, sftpDir);

我已经解决了

我从 developer branch 下载了源代码,这个 bug 已经修复了。然后我在 Visual Studio 2015 年打开 .NET Framework 3.5 项目并构建它。所以我获得了用于发布和调试的二进制文件、DLL。然后我回到我的 Visual Studio 2008 项目并添加此引用。我已经用相同的代码检查过,现在我可以使用相同的凭据连接到 FTP 服务器。

SSH.NET web 中提供的最后一个二进制文件不包含已修复的错误。