0

I am using SSH.NET to download file from the SFTP and here is my code:

string host = ConfigurationManager.AppSettings["SFTPDomain"];
string username = ConfigurationManager.AppSettings["SFTPUser"];
string password = ConfigurationManager.AppSettings["SFTPPass"];
string remoteFileName = ConfigurationManager.AppSettings["SFTPFileName"].ToString();

using (var sftp = new SftpClient(host, username, password))
{
    sftp.Connect();

    using (var file = File.OpenWrite(FilePath))
    {
        sftp.DownloadFile(remoteFileName, file);
    }

    sftp.Disconnect();
}

The problem is that the file which is csv is downloaded but it does not have any data inside. I changed the remoteFile path as well but still the file is downloaded with null data inside. I tried to check if file exists using

if (sftp.Exists(remoteFileName))
{
}

It always return true even if I change the remoteFileName with "pp".

Can anyone help me what I am doing wrong? Or recommend me another other library to download file from SFTP server. I have tried WinSCP but I am getting hostkey error so I tried to pass correct SshHostKeyFingerprint as guided by the server tutorial. Still I get the hostkey error. Is there any simple library i just need to download file from the SFTP?

4
  • already trying sshnet Commented Oct 7, 2014 at 5:51
  • check sshnet.codeplex.com/discussions/358307 Commented Oct 7, 2014 at 6:09
  • Regarding WinSCP host key problem: The tutorial you link to is about your account private key, not about server's (host) key. See WinSCP article on Where do I get SSH host key fingerprint for use with scripting or .NET assembly?. Note that SShNet default behavior to accept any host key is not secure. With SshNet, you should handle HostKeyReceived event and verify the host key. Key verification is enforced by WinSCP to upkeep security. Commented Oct 7, 2014 at 6:59
  • Did you try to debug the download (as SshNet is open source)? Do you have server-side logs? What is the server (software) you are connecting to? Commented Oct 7, 2014 at 7:05

3 Answers 3

3

I've seen the same issue. Using SSH.NET's ScpClient instead of SftpClient worked for me. It's a drop-in replacement:

using (ScpClient client = new ScpClient(host, username, password))
{
    client.Connect();

    using (Stream localFile = File.Create(localFilePath))
    {
         client.Download(remoteFilePath, localFile);
    }
}

With ScpClient you only get upload/download functionality instead of the many additional features of SFTP, but this may be good enough for your use-case.

Sign up to request clarification or add additional context in comments.

Comments

1

Try using:

using (Stream file = File.OpenWrite(FilePath))
{
    sftp.DownloadFile(remoteFileName, file);
}

You need to read the stream from the remote server before saving locally.

1 Comment

Kudos for a solution rather than a circumvention.
0

This is a very old question but thought I would put an answer here to save some effort for rookies like me who may encounter similar issue and just search up this question:

Essentially after you call the DownloadFile method and write into the stream, you have to reset the stream position back to beginning:

using (var sftp = new SftpClient(host, username, password))
{
    sftp.Connect();

    using (var file = File.OpenWrite(FilePath))
    {
        sftp.DownloadFile(remoteFileName, file);
        file.Position = 0;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.