Issue
I am using SSH.NET in my C# application to copy files from a Windows to a UNIX server, I have a few scenarios for this:
In the UNIX server directory if the file to be copied does not exist, then the modified datetime of the file when it is copied to the UNIX server changes to the copied datetime? Is this correct because the modified datetime is not supposed to change right?
In the UNIX Server directory if the file to be copied already exists, then on copying the same file which gets replaced in the UNIX Server path the modified datetime of the file does not change!
I am confused with this modified datetime as I have read in this post that SSH.NET does it wrongly, is this supposed to be right?
For those of you asking to provide code, here goes:
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;
}
}
Solution
The timestamp of the file on the remote SFTP server will always be set to the the time the remote file was modified the last time (i.e. the upload time) – As with any other file on a Linux server.
As the question you have linked yourself says:
after uploading files, the creation- and modified dates are altered to the time when the upload took place.
I assume you somehow expect that the local file timestamp is involved. It's not. You are not uploading a local file. You are uploading data from a stream (Stream
interface). SSH.NET (let alone SFTP server) does not even know that your instance of Stream
originated from a local file. So SSH.NET (let alone the SFTP server) cannot know the local file timestamp.
In the end, it behaves like if you have copied a file on the Linux server via a pipe (similar to a stream) instead of using cp
command, like:
cat source > target
The contents gets copied, but the timestamp will always be the time of the last modification (i.e. the copy time).
If you want the remote file timestamp to match timestamp of the source local file, you have to code that (like done in the question you know already):
SSH.NET: Is it possible to upload files using SFTP and preserve the file dates from source files?
Note that it's not true that "SSH.NET does it wrongly". It does what it should (could). It nowhere promises you to preserve the timestamp for you.
Answered By - Martin Prikryl Answer Checked By - Terry (WPSolving Volunteer)