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

时间:2017-04-11 19:43:55

标签: c# file-upload sftp ssh.net

我已经构建了一个控制台应用程序来将文件上传到FTP服务器。因为这个FTP服务器只接受SFTP协议,所以我使用SSH.NET开源库,因为.NET中没有内置方法(对于.NET 3.5,我在Visual Studio 2008 Professional下使用的那个)。< / p>

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

我正在使用从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日应用于develop Brank,但下载的最后一个二进制文件是2016年12月14日。那么我如何获得包含此修复程序的二进制文件?

2 个答案:

答案 0 :(得分:1)

我已经解决了。

我从开发人员分支下载了源代码,其中纠正了此错误。然后我在Visual Studio 2015中打开.NET Framework 3.5项目并构建它。所以我获得了二进制文件,DLL用于发布和调试。然后我回到我的Visual Studio 2008项目,并添加此引用。我现在用相同的代码检查了一下,我可以使用相同的凭据连接到FTP服务器。

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

答案 1 :(得分:-4)

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

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