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.
Without you providing any specific error message, it's hard to give specific suggestions.
However, I was using the same example and was getting a permissions exception on File.OpenWrite - using the localFileName variable, because using Path.GetFile was pointing to a location that obviously would not have permissions for opening a file > C:\ProgramFiles\IIS(Express)\filename.doc
I found that using System.IO.Path.GetFileName is not correct, use System.IO.Path.GetFullPath instead, point to your file starting with "C:\..."
Also open your solution in FileExplorer and grant permissions to asp.net for the file or any folders holding the file. I was able to download my file at that point.
My version of @Merak Marey's Code. I am checking if files exist already and different download directories for .txt and other files
While the example works, its not the correct way to handle the streams...
You need to ensure the closing of the files/streams with the using clause.. Also, add try/catch to handle IO errors...
A simple working code to download a file with SSH.NET library is:
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 previousFile.OpenWrite
call instead (that you are not using at all now):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 (combinelocalFileName
withsDir
?)