当前,我正在使用Renci SSH.NET库通过SFTP将文件上传到Unix服务器.我不喜欢的一件事是,在上传文件之后,创建日期和修改日期会更改为上传发生的时间.
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?
推荐答案SSH.NET库不会自动为您完成此操作.您必须对其进行编码.
The SSH.NET library won't do it for you automatically. You have to code it.
有SftpClient.SetLastWriteTime和SftpClient.SetLastWriteTimeUtc方法.但实际上它们尚未实现.
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);尽管由于SftpFileAttributes中缺少UTC API,但是如果客户端和服务器不在同一时区,则可能无法正确设置时间戳.
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.
有关更多详细信息,请参阅我的答复: 使用SSH.NET将文件从Windows移动到UNIX服务器时,修改的日期时间会更改
For more details, see my answer to: Modified date time changes when moving a file from Windows to UNIX server using SSH.NET
或者使用另一个能够自动保留时间戳的SFTP库,理想情况下具有UTC支持.
Or use another SFTP library capable for preserving the timestamp automatically, ideally with an UTC support.
例如, WinSCP .NET程序集会自动执行此操作.只需使用 Session.PutFiles方法:
For example, WinSCP .NET assembly does it automatically. Just use the Session.PutFiles method:
session.PutFiles(sourceFile, targetFile).Check();(我是WinSCP的作者)
更多推荐
SSH.NET:是否可以使用SFTP上传文件并保留源文件中的文件日期?
发布评论