Delphi는 자동 소프트웨어 업그레이드 기능을 실현합니다.
원칙은 간단합니다. 업데이트할 파일의 버전 번호를 기록하는 Update.ini 파일을 로컬로 유지합니다. 업데이트 프로그램이 시작될 때마다 Update.ini 파일이 생성됩니다. 먼저 FTP에서 로컬 이름으로 다운로드한 다음 두 파일을 비교합니다. 새 버전 번호가 이전 버전보다 크거나 새 파일이 이전 ini에 없으면 이는 파일을 나타냅니다. 업데이트한 후 하나씩 다운로드하세요.
이 프로그램의 이름은 AutoUpdate입니다. 이 exe를 생성한 다음 기본 프로그램과 함께 패키지하면 바탕 화면 바로 가기를 만들 때 기본 프로그램 대신 AutoUpdate를 가리킵니다.
로컬에 ini 파일도 있습니다. 예를 들어 ftp.ini라고 부르겠습니다. 내부 내용은 다음과 같습니다.
[정보]
메인=Project1.exe
param={app}sayyes.pj2 -y bde.txt
main=Project1.exe: 업그레이드 프로그램과 동일한 디렉터리에 있는 기본 프로그램의 이름입니다.
param={app}sayyes.pj2 -y bde.txt: 이것은 명령줄 매개변수이고, app은 현재 경로입니다. 프로그램에서 이를 대체하고 기본 프로그램에 전달합니다(필요한 경우).
update.ini의 콘텐츠 형식은 다음과 같습니다.
[뿌리]
사무실 문의.txt=20100519
[DB카드]
sayyes.pj2=20100519
FTP 사용자 비밀번호.txt=20100519
[root]는 루트 디렉터리를 나타내고, 다음 [dbcard]는 하위 디렉터리를 나타냅니다.
유닛 Main; 인터페이스는 Windows, 메시지, SysUtils, 변형, 클래스, 그래픽, 컨트롤, 양식, 대화 상자, StdCtrls, IdHTTP, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, ComCtrls, ExtCtrls, IniFiles, ShellAPI, jpeg를 사용합니다. 클래스(TForm) IdFTP1: IdHTTP1: TIdHTTP: TProgressBar; TGroupBox; ld_username: TLabeledEdit; TLabeledEdit; TProgressBar; TLabel; TListView; TLabel; 프로시저 IdFTP1Work(Sender: TObject; AWorkMode: TWorkMode; const AWorkCount: Integer); 프로시저 IdFTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode); ; private { 비공개 선언 } FSize:Integer; FPath: 문자열; FInitPath: FHandle:HWND; FMainExe: 문자열; function ConnectFTP:Boolean; *.dfm} //진행 절차 다운로드 TfrmMain.IdFTP1Work(Sender: TObject; AWorkMode: TWorkMode; const AWorkCount: Integer); start ProgressBar1.Position := AWorkCount; Procedure TfrmMain.IdFTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode); 시작 ProgressBar1.Position := 0; ProgressBar2.StepBy(1); end; 프로시저 TfrmMain.FormCreate(Sender: TfrmFlash); start Self.Visible := False; //플래시 화면을 추가할 수 없습니다. nil ); Application.ProcessMessages := ExtractFilePath(Application.ExeName); FIniFile := TIniFile.Create(FExePath+'ftp.ini'); //호스트, 포트 등의 정보를 로드합니다. LoadIni; Self.Visible := True; 마지막으로 FreeAndNil(frm); FParam := StringReplace(FParam,'{app}',FExePath,[rfReplaceAll]); //업데이트가 완료된 후 기본 프로그램을 시작하고 명령줄 매개변수를 전달합니다. ShellExecute(Handle,'open',PChar(FExePath+FMainExe) ,PChar(FParam),SW_NORMAL); Application.Terminate; //업데이트 목록 확인 end; TfrmMain.CheckUpdateList; i,ver,index:Integer; itemtr,itempath: start oldFile := TStringList.Create; /먼저 서버에서 update.ini 파일을 다운로드하고 로컬 update_new.ini에 저장합니다. IdFTP1.Get('update.ini',FExePath+'update_new.ini',True); FileExists(FExePath + 'update.ini') = False이면 종료; oldFile.LoadFromFile(FExePath + 'update.ini'); .LoadFromFile(FExePath + 'update_new.ini') itempath := ''; //아래 두 목록 비교를 시작합니다. newFile의 버전 번호가 oldFile의 버전 번호보다 크거나 oldFile에 버전 번호가 없으면 i := 0에서 newFile.Count로 업데이트해야 함을 의미합니다. - 1 start itemtr := newFile.Strings[i ]; if itemstr = '' then Continue; if itemtr[1] = '[' then start itempath := Copy(itemstr,2,Length(itemstr)-2); //루트 디렉토리인 경우 if itempath = 'root' then itempath := '/'; 계속; itemstr := newFile.Names[i]; index := oldFile.IndexOfName(itemstr); if index = - list_file.Items .Add; item.Caption := itemstr; item.SubItems.Add(itempath) end else start ver := StrToIntDef(newFile.Values[itemstr],0); ver > StrToIntDef(oldFile.Values[itemstr],0)이면 item 시작 := list_file.Items.Add; item.Caption := itemtr; itempath); end; list_file.Items.Count = 0인 경우 최종적으로; oldFile.Free; end; function TfrmMain.ConnectFTP: 시작 결과:= False; IdFTP1.Port := StrToIntDef(ld_port.Text,21) .사용자 이름 := ld_username.Text := ld_psw.Text; IdFTP1.Passive := cb_mode.ItemIndex = 1; FInitPath := IdFTP1.Connected; 제외 결과:= False; .DownLoadFile; var i:Integer; 경로:문자열; s1,s2:String; 시작 ProgressBar2.Max := list_file.Items.Count; ProgressBar2.Position := 0; for i := 0 to list_file.Items.Count 1 시작 Label4.Caption := 'Downloading'+list_file.Items[i].Caption; IdFTP1.ChangeDir(FInitPath); path := list_file.Items[i].SubItems.Strings[0]; path <>'/'인 경우 IdFTP1.ChangeDir(path); ForceDirectories(FExePath+path); 목록_파일.항목[i].캡션 s2 := FExePath+path+'/'+list_file.Items[i].Caption; IdFTP1.Get(s1,s2,True); end else start s1 := list_file.Items[i].Caption; list_file.Items[i].Caption; IdFTP1.Get(s1,s2,True); //실패한 항목 기록 FIniFile.WriteString('error',list_file.Items[i].Caption,'Success') end; //실패한 항목 기록 FIniFile.WriteString('error',list_file.Items[i]. Caption,'실패'); end; Label4.Caption := '모든 파일이 업데이트되었습니다! '; 삭제파일(FExePath+'update.ini'); CopyFile(PChar(FExePath+'update_new.ini'),PChar(FExePath+'update.ini'),False); end; FIniFile.ReadString('coninfo','host','******'); ld_username.Text := FIniFile.ReadString('coninfo','user','******') ld_psw.Text := FIniFile.ReadString('coninfo','psw','***** *'); ld_port.Text := FIniFile.ReadString('coninfo','port','21'); FIniFile.ReadInteger('coninfo','mode',1); FMainExe := FIniFile.ReadString('coninfo','main','Main.exe') FParam := FIniFile.ReadString('coninfo','param ',''); 종료; TfrmMain.SaveIni; FIniFile.WriteString('coninfo','host',ld_host.Text); FIniFile.WriteString('coninfo','user',ld_username.Text); FIniFile.WriteString('coninfo','psw',ld_psw.Text) ; FIniFile.WriteString('coninfo','port',ld_port.Text); FIniFile.WriteInteger('coninfo','mode',cb_mode.ItemIndex); 끝.
궁금한 점이 있으면 메시지를 남기거나 이 사이트의 커뮤니티에 가서 소통하고 토론하세요. 읽어주셔서 감사합니다. 이 사이트를 지원해 주셔서 감사합니다.