Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download files from SFTP with SSH.NET library

Tags:

c#

.net

ssh

sftp

string host = @"ftphost"; string username = "user"; string password = "********"; string localFileName  = System.IO.Path.GetFileName(@"localfilename"); string remoteDirectory = "/export/"; using (var sftp = new SftpClient(host, username, password)) {     sftp.Connect();     var files = sftp.ListDirectory(remoteDirectory);     foreach (var file in files)     {         if (!file.Name.StartsWith("."))         {             string remoteFileName = file.Name;             if (file.LastWriteTime.Date == DateTime.Today)              Console.WriteLine(file.FullName);              File.OpenWrite(localFileName);              string sDir = @"localpath";              Stream file1 = File.OpenRead(remoteDirectory + file.Name);             sftp.DownloadFile(remoteDirectory, file1);         }     } } 

I am using SSH.NET (Renci.SshNet) library to work with an SFTP server. What I need to do is grab files from a specific folder on the SFTP server based on today's date. Then copy those files from the SFTP server to a local drive a server of mine.

Above is the code I have but it is not working. Sometimes it says file does not exist but sometimes the files I will be downloading will not be on my local servers but I need to download whatever files were uploaded to the remote folder for that day.

Can someone take a look and see what is wrong? I believe it has something to do with the stream portion. I have worked with FTP much besides uploading files which I took some previous code I had and thought I could reverse the process but that isn't working. The code I used is based off of this example.

like image 215
user3195153 Avatar asked May 16 '14 19:05

user3195153


People also ask

Does .NET support SFTP?

NET Core doesn't support native SFTP or SCP protocols. Thus we need to borrow an external SFTP client library. Among a list of SFTP client libraries, SSH.NET is free and works fine for most use cases.


1 Answers

A simple working code to download a file with SSH.NET library is:

using (Stream fileStream = File.Create(@"C:\target\local\path\file.zip")) {     sftp.DownloadFile("/source/remote/path/file.zip", fileStream); } 

See also Downloading a directory using SSH.NET SFTP in C#.


To explain, why your code does not work:

The second parameter of SftpClient.DownloadFile is a stream to write a downloaded contents to.

You are passing in a read stream instead of a write stream. And moreover the path you are opening read stream with is a remote path, what cannot work with File class operating on local files only.

Just discard the File.OpenRead line and use a result of previous File.OpenWrite call instead (that you are not using at all now):

Stream file1 = File.OpenWrite(localFileName);  sftp.DownloadFile(file.FullName, file1); 

Or even better, use File.Create to discard any previous contents that the local file may have.

I'm not sure if your localFileName is supposed to hold full path, or just file name. So you may need to add a path too, if necessary (combine localFileName with sDir?)

like image 85
Martin Prikryl Avatar answered Sep 28 '22 04:09

Martin Prikryl