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)