0 votes
by (120 points)
edited

Hi,

I would like to preserve the original creation date/time and modified date/time after a file has been transfered.

Is there a way of doing this? I know WinSCP offers this option, so I assume it is possible

Regards, Christian

Applies to: Rebex SFTP

1 Answer

0 votes
by (148k points)
edited

Yes, this is possible using the following code (C#):

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);

(If you prefer VB.NET, please let me know.)

Please be aware that creation date/time will only be set if the SFTP server supports SFTP v4. On servers that only support SFTP v3 (such as OpenSSH), only modified date/time will be set (this is a limitation of SFTP v3).

...