【问题标题】:.NET FTP Upload file and preserve original date time.NET FTP 上传文件并保留原始日期时间
【发布时间】:2016-01-26 23:49:22
【问题描述】:

我们有一个带有 FTP over SSL 的 Windows 2008 R2 Web 服务器。此应用程序使用 .NET 4.5,当我上传文件时,文件上的日期/时间更改为服务器上的当前日期/时间。有没有办法让上传的文件保留原始(最后修改)日期?

这是我所拥有的:

FtpWebRequest clsRequest = (FtpWebRequest)WebRequest.Create(FTPFilePath);
clsRequest.EnableSsl = true;
clsRequest.UsePassive = true;
clsRequest.Credentials = new NetworkCredential(swwwFTPUser, swwwFTPPassword);
clsRequest.Method = WebRequestMethods.Ftp.UploadFile;
Byte[] bFile = File.ReadAllBytes(LocalFilePath);
Stream clsStream = clsRequest.GetRequestStream();
clsStream.Write(bFile, 0, bFile.Length);
clsStream.Close();
clsStream.Dispose();
clsRequest = null;

【问题讨论】:

  • 我手边没有 ftp 服务器,但是如果在发送之前先压缩它会怎样?也许它会重写 zip 文件的属性但不理会内容?看这里:stackoverflow.com/questions/2960719/…

标签: c# .net ftp ftpwebrequest


【解决方案1】:

实际上没有标准的方法可以通过 FTP 协议更新远程文件的时间戳。这可能是FtpWebRequest 不支持它的原因。

更新时间戳有两种非标准方式。要么是非标准的MFMT 命令:

MFMT yyyymmddhhmmss path

或非标准使用(否则为标准)MDTM 命令:

MDTM yyyymmddhhmmss path

FtpWebRequest 也不允许您发送自定义命令。

参见例如How to send arbitrary FTP commands in C#


所以你必须使用第 3 方 FTP 库。

例如WinSCP .NET assembly默认保留上传文件的时间戳。

// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "example.com",
    UserName = "user",
    Password = "mypassword",
};

using (Session session = new Session())
{
    // Connect
    session.Open(sessionOptions);

    // Upload
    session.PutFiles(@"c:\toupload\file.txt*", "/home/user/").Check();
}

a full example

请注意,WinSCP .NET 程序集不是本机 .NET 程序集。它是一个围绕控制台应用程序的瘦 .NET 包装器。

(我是 WinSCP 的作者)

【讨论】:

  • 使用 MFMT 或 MDTM 命令似乎是我需要走的方向。我不想在这些服务器上使用第三方工具,但简单性会很好。如果第三方工具发现了它,它必须是可能的。感谢您的信息。
【解决方案2】:

我知道我们可以分配文件属性:-

//Change the file created time.
File.SetCreationTime(path, dtCreation);
//Change the file modified time.
File.SetLastWriteTime(path, dtModified);

如果您可以在将原始日期保存到服务器之前提取它,那么您可以更改文件属性....类似这样的:-

Sftp sftp = new Sftp();
sftp.Connect(...);
sftp.Login(...);

// upload the file
sftp.PutFile(localFile, remoteFile);

// assign creation and modification time attributes
SftpAttributes attributes = new SftpAttributes();
System.IO.FileInfo info = new System.IO.FileInfo(localFile);
attributes.Created = info.CreationTime;
attributes.Modified = info.LastWriteTime;

// set attributes of the uploaded file
sftp.SetAttributes(remoteFile, attributes);

我希望这会为您指明正确的方向。

【讨论】:

  • 问题是关于 FTP,你的答案是关于 SFTP。而且你甚至没有提到你的代码使用的是什么 SFTP 库。
  • 我觉得这个概念是对的,把文件放到服务器上,然后尝试更改修改日期。但是对于基于 SSL 的 FTP,我可能必须使用 System.Net.Sockets TcpClient() 通过 TransmitCommand() 并使用 MFMT 或 MDTM 命令设置上传的文件修改日期。喜欢这篇文章:stackoverflow.com/questions/2321097/…
【解决方案3】:

这是一个较老的问题,但我将在此处添加我的解决方案。我使用了类似于@Martin Prikryl 提出的解决方案的方法,使用MDTM 命令。他的回答将DateTime 格式字符串显示为yyyymmddhhmmss,这是不正确的,因为它不能正确处理月份和24 小时时间格式。在这个答案中,我更正了这个问题并使用 C# 提供了完整的解决方案。

我使用了FluentFTP 库,它很好地处理了通过 C# 处理 FTP 的许多其他方面。要设置修改时间,这个库不支持它,但它有一个Execute 方法。使用FTP命令MDTM yyyyMMddHHmmss /path/to/file.txt会设置文件的修改时间。

注意:在我的例子中,我需要使用通用时间,你可能就是这种情况。

下面的代码展示了如何使用Execute方法和发送MDTM命令连接到FTP并设置最后修改时间。

FtpClient client = new FtpClient("ftp-address", "username", "password");
client.Connect();

FtpReply reply = client.Execute($"MDTM {DateTime.UtcNow.ToString("yyyyMMddHHmmss")} /path/to/file.txt");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2015-03-18
    相关资源
    最近更新 更多