We have a Windows 2008 R2 web server with FTP over SSL. This app uses .NET 4.5 and when I upload files, the date/time on the file changes to the current date/time on the server. Is there a way to have the uploaded file preserve the original (last modified) date?
我们有一台带有FTP over SSL的Windows 2008 R2 Web服务器。此应用程序使用.NET 4.5,当我上传文件时,文件上的日期/时间将更改为服务器上的当前日期/时间。有没有办法让上传的文件保留原始(最后修改)日期?
Here is what I have:
这是我有的:
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;
2 个解决方案
#1
2
There's really no standard way to update timestamp of a remote file over an FTP protocol. That's probably why the FtpWebRequest
does not support it.
实际上没有标准方法来通过FTP协议更新远程文件的时间戳。这可能就是FtpWebRequest不支持它的原因。
There are two non-standard ways to update the timestamp. Either a non-standard MFMT
command:
有两种非标准方法可以更新时间戳。非标准MFMT命令:
MFMT yyyymmddhhmmss path
or a non-standard use of (otherwise standard) MDTM
command:
或非标准使用(否则为标准)MDTM命令:
MDTM yyyymmddhhmmss path
But the FtpWebRequest
does not allow you to send a custom command either.
但是FtpWebRequest也不允许您发送自定义命令。
See for example How to send arbitrary FTP commands in C#.
请参阅例如如何在C#中发送任意FTP命令。
So you have to use a 3rd party FTP library.
所以你必须使用第三方FTP库。
For example WinSCP .NET assembly preserves a timestamp of an uploaded file by default.
例如,WinSCP .NET程序集默认保留上载文件的时间戳。
// 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();
}
See a full example.
查看完整示例。
Note that WinSCP .NET assembly is not a native .NET assembly. It's rather a thin .NET wrapper around a console application.
请注意,WinSCP .NET程序集不是本机.NET程序集。它是一个围绕控制台应用程序的瘦.NET包装器。
(I'm the author of WinSCP)
(我是WinSCP的作者)
#2
2
I know that we can assign file attributes:-
我知道我们可以分配文件属性: -
//Change the file created time.
File.SetCreationTime(path, dtCreation);
//Change the file modified time.
File.SetLastWriteTime(path, dtModified);
If you can extract the original date before you save it to the server, then you can change file attributes....something like this:-
如果您可以在将原始日期保存到服务器之前提取原始日期,则可以更改文件属性....如下所示: -
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);
I hope this points you in the right direction.
我希望这能指出你正确的方向。
#1
2
There's really no standard way to update timestamp of a remote file over an FTP protocol. That's probably why the FtpWebRequest
does not support it.
实际上没有标准方法来通过FTP协议更新远程文件的时间戳。这可能就是FtpWebRequest不支持它的原因。
There are two non-standard ways to update the timestamp. Either a non-standard MFMT
command:
有两种非标准方法可以更新时间戳。非标准MFMT命令:
MFMT yyyymmddhhmmss path
or a non-standard use of (otherwise standard) MDTM
command:
或非标准使用(否则为标准)MDTM命令:
MDTM yyyymmddhhmmss path
But the FtpWebRequest
does not allow you to send a custom command either.
但是FtpWebRequest也不允许您发送自定义命令。
See for example How to send arbitrary FTP commands in C#.
请参阅例如如何在C#中发送任意FTP命令。
So you have to use a 3rd party FTP library.
所以你必须使用第三方FTP库。
For example WinSCP .NET assembly preserves a timestamp of an uploaded file by default.
例如,WinSCP .NET程序集默认保留上载文件的时间戳。
// 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();
}
See a full example.
查看完整示例。
Note that WinSCP .NET assembly is not a native .NET assembly. It's rather a thin .NET wrapper around a console application.
请注意,WinSCP .NET程序集不是本机.NET程序集。它是一个围绕控制台应用程序的瘦.NET包装器。
(I'm the author of WinSCP)
(我是WinSCP的作者)
#2
2
I know that we can assign file attributes:-
我知道我们可以分配文件属性: -
//Change the file created time.
File.SetCreationTime(path, dtCreation);
//Change the file modified time.
File.SetLastWriteTime(path, dtModified);
If you can extract the original date before you save it to the server, then you can change file attributes....something like this:-
如果您可以在将原始日期保存到服务器之前提取原始日期,则可以更改文件属性....如下所示: -
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);
I hope this points you in the right direction.
我希望这能指出你正确的方向。