2

Currently, I am using the Renci SSH.NET library to upload files to a Unix server using SFTP. One thing that I don't like is that after uploading files, the creation- and modified dates are altered to the time when the upload took place.

I would like to preserve the original file dates from the source files, is that possible?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Erik
  • 2,316
  • 9
  • 36
  • 58

1 Answers1

7

The SSH.NET library won't do it for you automatically. You have to code it.

There are SftpClient.SetLastWriteTime and SftpClient.SetLastWriteTimeUtc methods. But they are actually not implemented yet.

You can code it like this instead:

SftpFileAttributes fileAttributes = client.GetAttributes(targetFile);
fileAttributes.LastWriteTime = File.GetLastWriteTime(sourceFile);
client.SetAttributes(targetFile, fileAttributes);

Though due to a lack of UTC API in the SftpFileAttributes, you might have problems setting the timestamp correctly, if a client and a server are not in the same timezone.


For more details, see my answer to:
Modified date time changes when moving a file from Windows to UNIX server using SSH.NET


Or use another SFTP library capable for preserving the timestamp automatically, ideally with an UTC support.

For example, WinSCP .NET assembly does it automatically. Just use the Session.PutFiles method:

session.PutFiles(sourceFile, targetFile).Check();

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992