我在我的 C# 应用程序中使用 SSH.NET 将文件从 Windows 复制到 UNIX 服务器,我有几个方案:
在UNIX服务器目录如果要复制的文件不存在,则文件复制到时的修改日期时间UNIX 服务器更改为复制的日期时间?这是正确的,因为修改后的日期时间不应该正确更改吗?
在 UNIX 服务器目录中如果要复制的文件已经存在,则在复制相同文件时,该文件在 UNIX 服务器路径中被替换文件修改日期时间不变!
我对我在 post 中读到的这个修改后的日期时间感到困惑SSH.NET 做错了,这应该是正确的吗?
对于那些要求提供代码的人,这里是:
private static int UploadFileToSFTP (string localFileFullPath, string uploadPath)
{
try
{
Log.Debug("Inside Utilities.UploadFileToSFTP() with localFileFullPath=" + localFileFullPath + ", and remoteUploadPath=" + uploadPath);
Log.Debug("Uploading File : " + uploadPath);
using (FileStream fs = new FileStream(localFileFullPath, FileMode.Open))
{
Log.Debug("Checking if path: " + Path.GetDirectoryName(uploadPath).Replace("\\", "/") + " already exists");
if (!IsDirectoryExists(Path.GetDirectoryName(uploadPath).Replace("\\", "/")))
{
Log.Debug(Path.GetDirectoryName(uploadPath).Replace("\\", "/") + " | Directory does not exist, creating!");
sftpClient.CreateDirectory(Path.GetDirectoryName(uploadPath).Replace("\\", "/"));
}
else
{
Log.Debug(Path.GetDirectoryName(uploadPath).Replace("\\", "/") + " | Directory already exists!");
}
Log.Debug("Checking if file: " + uploadPath + " already exists");
if (sftpClient.Exists(uploadPath))
{
Log.Debug(uploadPath + " | File Already exists in the Server");
}
else
{
Log.Debug(uploadPath + " | File Does not exist in the Server!");
}
sftpClient.BufferSize = 1024;
sftpClient.UploadFile(fs, uploadPath);
fs.Close();
}
return 1;
}
catch (Exception exception)
{
Log.Error("Error in Utilities.UploadFileToSFTP(): ", exception);
return 0;
}
}
最佳答案
远程 SFTP 服务器上文件的时间戳将始终设置为上次修改远程文件的时间(即上传时间)——与 Linux 服务器上的任何其他文件一样。
作为question you have linked yourself说:
after uploading files, the creation- and modified dates are altered to the time when the upload took place.
我假设您以某种方式期望涉及本地文件时间戳。它不是。您没有上传本地文件。您正在从流(Stream
接口(interface))上传数据。 SSH.NET(更不用说 SFTP 服务器了)甚至不知道您的 Stream
实例源自本地文件。所以 SSH.NET(更不用说 SFTP 服务器)无法知道本地文件时间戳。
最后,它的行为就像您通过管道(类似于流)在 Linux 服务器上复制了一个文件,而不是使用 cp
命令,例如:
cat source > target
内容被复制,但时间戳始终是最后一次修改的时间(即复制时间)。
如果您希望远程文件时间戳与源本地文件的时间戳相匹配,您必须编写代码(就像在您已经知道的问题中所做的那样):
SSH.NET: Is it possible to upload files using SFTP and preserve the file dates from source files?
请注意,“SSH.NET 做错了” 是不正确的。它做了它应该(可能)做的事。它无处 promise 为您保留时间戳。
关于c# - 使用 SSH.NET 将文件从 Windows 移动到 UNIX 服务器时修改的日期时间发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53063696/