c# - 使用 SSH.NET 将文件从 Windows 移动到 UNIX 服务器时修改的日期时间发生变化

标签 c# linux unix sftp ssh.net

我在我的 C# 应用程序中使用 SSH.NET 将文件从 Windows 复制到 UNIX 服务器,我有几个方案:

  1. 在UNIX服务器目录如果要复制的文件不存在,则文件复制到时的修改日期时间UNIX 服务器更改为复制的日期时间?这是正确的,因为修改后的日期时间不应该正确更改吗?

  2. 在 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/

相关文章:

c# - 组织大型 C# 解决方案

linux - 从 azure 上的 Linux 服务器创建 docker 镜像

c# - 在 C# 中模拟 linux openssl 命令

linux - Perl 升级会破坏 Linux 上的旧版本吗?

c# - 如何识别拼写不同的相似词

c# - DynamicResources 无法加载到以编程方式创建的控件中

c# - 使用 SendMessage 击败 'A program is trying to access email'

c - 可变堆栈大小

linux - 使用 awk 分割数据但缺少列名

linux - 验证来自 bash 的输入