I have built a console application to upload a file to a FTP server. As this FTP server only accepts SFTP protocol I am using SSH.NET open source library since there are no built in method in .NET (At less for .NET 3.5, which is the one I am using under Visual Studio 2008 Professional).
I have seen a lot of examples. I have implemented a quick test application to connect to a public SFTP server and upload a file using SFTP protocol.
I am using below public free SFTP server obtained from here:
server : demo.wftpserver.com
user : demo-user
password : demo-user
port : 2222
And here is the code:
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);
}
The problem is the following:
When trying to connect with the server using sentence below:
client.Connect();
I get an exception. The exception says the following:
"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
Also, I have tried to change the timeout as explained here but without success.
However, If I try to connect to the same SFTP server using the same credentials from FileZilla client, then I can connect without problems and even upload a file.
So I do not understand what is happening. Could someone help me?
UPDATE: I have found this fix. The patch was applied in develop brank on 22 January 2017 but the last binary for download is 14th December 2016. So how can I get the binaries that includes this fix?