Reputation: 184
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!
Upvotes: 5
Views: 3867
Reputation: 202088
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 to ls
*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 FTP MDTM
command) to individually retrieve timestamps for each file. An advantage is that the response is standardized by the RFC 3659 to YYYYMMDDHHMMSS[.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
var localPath = @"d:\www";
var remotePath = "/home/martin/public_html";
session.SynchronizeDirectories(
SynchronizationMode.Local, localPath, remotePath, false).Check();
}
WinSCP GUI can generate a code template for you.
(I'm the author of WinSCP)
Upvotes: 5
Reputation:
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
Upvotes: 2