SSH.NET:是否可以使用SFTP上传文件并保留源文件中的文件日期?

编程入门 行业动态 更新时间:2024-10-27 00:36:09
本文介绍了SSH.NET:是否可以使用SFTP上传文件并保留源文件中的文件日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当前,我正在使用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上传文件并保留源文件中的文件日期?

本文发布于:2023-11-05 04:21:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:源文件   可以使用   上传文件   日期   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!