C# - Download files from FTP which have higher last-modified date -
i have ftp server files. have same files in in local directory (in c:\
).
when i'll run program, i'd searches files in ftp server have last-modified timestamp later same file (equal name) in local directory , download files founded.
can give me or tip, please? i'll appreciate answers!
unfortunately, there's no reliable , efficient way retrieve timestamps using features offered .net framework not support ftp mlsd
command. mlsd
command provides listing of remote directory in standardized machine-readable format. command , format standardized rfc 3659.
alternatives can use supported .net framework:
the
listdirectorydetails
method (the ftplist
command) retrieve details of files in directory , deal ftp server specific format of details (*nix format similarls
*nix command common, drawback format may change on time, newer files "may 8 17:48" format used , older files "oct 18 2009" format used).examples:
dos/windows format: c# class parse webrequestmethods.ftp.listdirectorydetails ftp response
*nix format: parsing ftpwebrequest listdirectorydetails linethe
getdatetimestamp
method (an ftpmdtm
command) individually retrieve timestamps each file. advantage response standardized rfc 3659yyyymmddhhmmss[.sss]
. disadvantage have send separate request each file, can 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 can use 3rd party ftp client implementation supports modern mlsd
command.
for example winscp .net assembly supports that.
you can use session.listdirectory
or session.enumerateremotefiles
methods , read remotefileinfo.lastwritetime
of files in returned collection.
or easier, can use session.synchronizedirectories
have library automatically download (synchronize) 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(); }
(i'm author of winscp)
Comments
Post a Comment