问题
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!
回答1:
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. The MLSD
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 FTP
LIST
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 linethe GetDateTimestamp method (an FTP
MDTM
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.const string uri = "ftp://example.com/remote/path/file.txt"; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); request.Method = WebRequestMethods.Ftp.GetDateTimestamp; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("{0} {1}", uri, response.LastModified);
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 the Session.EnumerateRemoteFiles methods and read the RemoteFileInfo.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:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Synchronize files
session.SynchronizeDirectories(
SynchronizationMode.Local, @"d:\www", "/home/martin/public_html", false).Check();
}
WinSCP GUI can generate a code template for you.
(I'm the author of WinSCP)
回答2:
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
来源:https://stackoverflow.com/questions/38073302/c-sharp-download-files-from-ftp-which-have-higher-last-modified-date