给你一个用Ftp下载更新的Demo:
program Update;
uses
Windows, SysUtils, IdFTPList, IdAllFTPListParsers, IdFTP;
{$R *.res}
function CovFileDate(Fd:_FileTime): TDateTime;
//转换文件的时间格式
var
Tct: _SYSTEMTIME;
Temp: _FILETIME;
begin
FileTimeToLocalFileTime(Fd,Temp);
FileTimeToSystemTime(Temp,Tct);
CovFileDate := SystemTimeToDateTime(Tct);
end;
procedure begin
Update;
var
IdFTP1: TIdFTP;
i: Integer;
F: TSearchRec;
ExeFileLastDate: TDateTime;
begin
ExeFileLastDate := Now();
//先得到客户端可执行文件的修改时间
if FindFirst('loyalty.exe', 63, F) = 0 then
begin
repeat
if (F.Attr and 63) = F.Attr then
ExeFileLastDate := CovFileDate(F.FindData.ftLastWriteTime);
until FindNext(F) <> 0;
FindClose(F);
end;
IdFTP1 := TIdFTP.Create(nil);
IdFTP1.Host := '192.168.1.19';
IdFTP1.Username := 'test';
IdFTP1.Password := '123456';
if not IdFTP1.Connected then
IdFTP1.Connect();
IdFTP1.ChangeDir('UpdateFile');
IdFTP1.List;
for i := 0 to IdFTP1.DirectoryListing.Count - 1do
//遍历 Ftp 服务器
if IdFTP1.DirectoryListing.Items.ItemType = ditFile then
if (IdFTP1.DirectoryListing.Items.ModifiedDate * 24 + 8) / 24 > ExeFileLastDate then
IdFTP1.Get(IdFTP1.DirectoryListing.Items.FileName, IdFTP1.DirectoryListing.Items.FileName, True);
IdFTP1.Disconnect;
end;
begin
try
begin
Update;
finally
WinExec(PChar(ExtractFilePath(ParamStr(0)) + 'demo.exe ' + ParamStr(1)), SW_NORMAL);
Halt;
end;
end.