SftpClient class, UploadFile vs WriteAllBytes methods

Aug 25, 2012 at 1:34 PM
Edited Aug 25, 2012 at 1:36 PM

Hi,

I'm new to SSH.NET library. I couldn't find documentation that describes SftpClient methods, so I'd appreciate any help regarding UploadFile and WriteAllBytes methods differences (if they are comparable in the first place).

With following code I was able to upload any file size to SFTP server (I'm using excellent FreeSSHd server):

PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("testServer", 22, "test", "test");

string filePath = @"C:\testUpload\40KB.txt";

using (var sftpClient = new SftpClient(connectionInfo))
{
     sftpClient.Connect();

     byte[] uploadData = File.ReadAllBytes(filePath);
     
     sftpClient.WriteAllBytes("WriteAllBytesMethodExample.txt", uploadData);

     sftpClient.Disconnect();
}

But, as stated in http://sshnet.codeplex.com/discussions/254718, I can't upload more than 32768 bytes with following code:

using (var sftpClient = new SftpClient(connectionInfo))
{
     sftpClient.Connect();

     FileStream fileToUpload = new FileStream(filePath, FileMode.Open);

     sftpClient.UploadFile(fileToUpload, "UploadFileMethodExample.txt");

     fileToUpload.Close();

     sftpClient.Disconnect();
}

 

What is the proper way to upload large file? When to use WriteAllBytes, and when UploadFile method?

 

Best regards,

Hrvoje