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?

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.