I have an FTP server with some files. I have the same files in in a local directory (in C:\
).
When I'll run the program, I'd like it searches all files in the FTP server that have a last-modified timestamp later than the same file (equal name) in local directory and download all files founded.
Can someone give me a help or a tip, please? I'll appreciate all answers!
Unfortunately, there's no really reliable and efficient way to retrieve timestamps using features offered by the .NET framework, as it does not support the FTP
MLSD
command. TheMLSD
command provides listing of remote directory in a standardized machine-readable format. The command and the format is standardized by the RFC 3659.Alternatives you can use that are supported by the .NET framework:
the
ListDirectoryDetails
method (the FTPLIST
command) to retrieve details of all files in a directory and then you deal with FTP server specific format of the details (*nix format similar tols
*nix command is the most common, drawback is that the format may change over time, as for newer files "May 8 17:48" format is used and for older files "Oct 18 2009" format is used).Examples:
DOS/Windows format: C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response
*nix format: Parsing FtpWebRequest ListDirectoryDetails line
the
GetDateTimestamp
method (an FTPMDTM
command) to individually retrieve timestamps for each file. An advantage is that the response is standardized by the RFC 3659 toYYYYMMDDHHMMSS[.sss]
. A disadvantage is that you have to send a separate request for each file, what can be quite inefficient.Alternatively you can use a 3rd party FTP client implementation that supports the modern
MLSD
command.For example the WinSCP .NET assembly supports that.
You can use the
Session.ListDirectory
or theSession.EnumerateRemoteFiles
methods and read theRemoteFileInfo.LastWriteTime
of the files in returned collection.Or even easier, you can use the
Session.SynchronizeDirectories
to have the library automatically download (synchronize) the modified files:WinSCP GUI can generate a code template for you.
(I'm the author of WinSCP)
List all files :
https://msdn.microsoft.com/en-us/library/ms229716(v=vs.110).aspx
Read date :
https://msdn.microsoft.com/en-us/library/system.net.ftpwebresponse.lastmodified(v=vs.110).aspx