使用 SSH.NET 将文件从 Windows 移动到 UNIX 服务器时修改日期时间发生变化
Modified date time changes when moving a file from Windows to UNIX server using SSH.NET
我在我的 C# 应用程序中使用 SSH.NET 将文件从 Windows 复制到 UNIX 服务器,我有几个方案:
在UNIX服务器目录下如果要复制的文件不存在,则复制到UNIX服务器时文件的修改日期时间更改为复制的日期时间?这是正确的吗,因为修改后的日期时间不应该正确更改?
在UNIX Server目录下如果要复制的文件已经存在,那么就复制同一个文件在 UNIX 服务器路径中被替换 文件的修改日期时间没有改变!
我对这个修改后的日期时间感到困惑,因为我在这篇 中读到 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 上的任何其他文件一样服务器。
正如 所说:
after uploading files, the creation- and modified dates are altered to the time when the upload took place.
我假设您以某种方式期望涉及本地文件时间戳。不是。您没有上传本地文件。您正在从流中上传数据(Stream
接口)。 SSH.NET(更不用说 SFTP 服务器)甚至不知道您的 Stream
实例来自本地文件。所以SSH.NET(更不用说SFTP服务器了)无法知道本地文件的时间戳。
最后,它的行为就像您通过管道(类似于流)在 Linux 服务器上复制了一个文件,而不是使用 cp
命令,例如:
cat source > target
内容被复制,但时间戳将始终是最后一次修改的时间(即复制时间)。
如果您希望远程文件时间戳与源本地文件的时间戳相匹配,您必须编写代码(就像在您已经知道的问题中所做的那样):
请注意,SSH.NET 做错了是不正确的。它做了它应该(可能)做的事。它无处承诺为您保留时间戳。
我在我的 C# 应用程序中使用 SSH.NET 将文件从 Windows 复制到 UNIX 服务器,我有几个方案:
在UNIX服务器目录下如果要复制的文件不存在,则复制到UNIX服务器时文件的修改日期时间更改为复制的日期时间?这是正确的吗,因为修改后的日期时间不应该正确更改?
在UNIX Server目录下如果要复制的文件已经存在,那么就复制同一个文件在 UNIX 服务器路径中被替换 文件的修改日期时间没有改变!
我对这个修改后的日期时间感到困惑,因为我在这篇
对于那些要求提供代码的人,这里是:
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 上的任何其他文件一样服务器。
正如
after uploading files, the creation- and modified dates are altered to the time when the upload took place.
我假设您以某种方式期望涉及本地文件时间戳。不是。您没有上传本地文件。您正在从流中上传数据(Stream
接口)。 SSH.NET(更不用说 SFTP 服务器)甚至不知道您的 Stream
实例来自本地文件。所以SSH.NET(更不用说SFTP服务器了)无法知道本地文件的时间戳。
最后,它的行为就像您通过管道(类似于流)在 Linux 服务器上复制了一个文件,而不是使用 cp
命令,例如:
cat source > target
内容被复制,但时间戳将始终是最后一次修改的时间(即复制时间)。
如果您希望远程文件时间戳与源本地文件的时间戳相匹配,您必须编写代码(就像在您已经知道的问题中所做的那样):
请注意,SSH.NET 做错了是不正确的。它做了它应该(可能)做的事。它无处承诺为您保留时间戳。