SSH.NET: Is it possible to upload files using SFTP and preserve the file dates from source files?

旧街凉风 提交于 2019-12-06 05:16:35

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!