Search code examples
c#sshcharacter-encodingsftpssh.net

Renci SSH.NET: character encoding when uploading files


I am currently using Renci.SshNet to upload files and directories using SFTP, and everything works fine so far, except for encoding issues when copying files containing special character, like the German letters ß, ä. ö. ü, and so on.

For example, when I try to upload a directory called "Fünf", the name is transcripted to "Fünf".

Is there any kind of encoding I need to enable or set so that my strings will arrive correctly?

using (var sftp = new SftpClient(host, username, password))
{
      sftp.Connect();
      Stream file3 = File.OpenRead(localFileName);
      var result = sftp.BeginUploadFile(file3, remoteFileName) as SftpUploadAsyncResult;
}

Solution

  • The SSH.NET defaults to UTF-8 encoding.

    Your SFTP server possibly uses a different encoding (maybe ISO-8859-1).

    To make SSH.NET use a non-UTF-8 encoding, use an overload of SftpClient constructor that takes ConnectionInfo:

    public SftpClient(ConnectionInfo connectionInfo)
    

    And set the ConnectionInfo.Encoding as needed.


    Example code that you ended up using:

    var meth = new PasswordAuthenticationMethod(username, password);
    ConnectionInfo myConnectionInfo = new ConnectionInfo(host, 22, username, meth); 
    myConnectionInfo.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
    
    using (var sftp = new SftpClient(myConnectionInfo))
    { 
        sftp.Connect();
        ....
    }