c# - Download the latest file from an FTP server -
i have download latest file ftp server. know how download latest file computer, don't how download ftp server.
how can download latest file ftp server?
this program download latest file computer
public form1() { initializecomponent(); string startfolder = @"c:\users\user3\desktop\documentos xml"; system.io.directoryinfo dir = new system.io.directoryinfo(startfolder); ienumerable<system.io.fileinfo> filelist = dir.getfiles("*.*", system.io.searchoption.alldirectories); ienumerable<system.io.fileinfo> filequerry = file in filelist file.extension == ".txt" orderby file.creationtimeutc select file; foreach (system.io.fileinfo fi in filequerry) { var newestfile = (from file in filequerry orderby file.creationtimeutc select new { file.fullname, file.name }) .first(); textbox2.text = newestfile.fullname; } }
ok, code know date of last file, how know name of file????????
you have retrieve timestamps of remote files select latest one.
modification timestamp of files in directory using features offered .net framework, not support ftp mlsd
. mlsd
command provides listing of remote directory in standardized machine-readable format. command , format standardized rfc 3659.
alternatives can use, supported .net framework:
listdirectorydetails
method (the ftplist
command) retrieve details of files in directory , deal ftp server specific format of detailsdos/windows format: c# class parse webrequestmethods.ftp.listdirectorydetails ftp response
*nix format: parsing ftpwebrequest listdirectorydetails linegetdatetimestamp
method (the ftpmdtm
command) individually retrieve timestamps each file. advantage response standardized rfc 3659yyyymmddhhmmss[.sss]
. disadvantage have send separate request each file, can quite inefficient. method useslastmodified
property mention: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.
there's example specific task: downloading recent file.
example powershell , sftp, translates c# , ftp easily:
// setup session options sessionoptions sessionoptions = new sessionoptions { protocol = protocol.ftp, hostname = "example.com", username = "username", password = "password", }; using (session session = new session()) { // connect session.open(sessionoptions); // list of files in directory string remotepath = "/remote/path/"; remotedirectoryinfo directoryinfo = session.listdirectory(remotepath); // select recent file remotefileinfo latest = directoryinfo.files .orderbydescending(file => file.lastwritetime) .first(); // download selected file localpath = @"c:\local\path\"; session.getfiles(session.escapefilemask(remotepath + latest.name), localpath).check(); }
(i'm author of winscp)
Comments
Post a Comment