Thanks Martin! Indeed, I forgot to dispose the stream...
- DaveT
Before posting, please read how to report bug or request support effectively.
Bug reports without an attached log file are usually useless.
sftpFile.Content setter does. But you need to dispose the stream returned by Session.GetFile, before you can use the Session instance again. That's true even for calls to Close or Dispose (implied by the using).
session.GetFile the stream has errors you can see in the attached file StreamErrors.PNG.
session.Close the following exception is thrown:
Exception:
System.InvalidOperationException: 'Recursive calls not allowed'
Stack:
at WinSCP.Lock.Enter()
at WinSCP.Session.Dispose()
at ..DownloadFile(String remotePath) in ..\Sftp.cs:line 97
using (var session = new Session())
{
var sessionOptions = this.InitializeSessionOptions();
session.Open(sessionOptions);
session.DebugLogPath = @"c:\test\log.txt";
var remoteFile = session
.ListDirectory(remotePath)
.Files
.Where(x => x.IsDirectory == false)
.OrderByDescending(x => x.LastWriteTime)
.FirstOrDefault();
if (remoteFile != null)
{
sftpFile.Name = remoteFile.Name;
sftpFile.Content = session.GetFile(remoteFile.FullName);
}
session.Close();
}
session.GetFileToDirectory and it works perfectly fine.