delphi 让程序自己更新本程序


本文整理自网络,侵删。

 先介绍一下 MOVEFILEEX 的使用.

移动文件:

MoveFileEx('c:\winnt\system32\xxxx.exe', 'd:\winnt.bak\system32\xxxx.exe',MOVEFILE_REPLACE_EXISTING);
MoveFileEx('c:\winnt\system32\xxxx.exe', 'd:\winnt.bak\system32\xxxx.exe',MOVEFILE_DELAY_UNTIL_REBOOT);


删除文件:

MoveFileEx('c:winnt\system32\xxxx.exe', nil,MOVEFILE_REPLACE_EXISTING);
MoveFileEx('c:winnt\system32\xxxx.exe', nil,MOVEFILE_DELAY_UNTIL_REBOOT);

我们再来看看用什么方法来实现程序自身的更新.

//拷贝副本,更新完删除副本,刚测试好


procedure StartUpdate;
var
vBatchFile: TextFile;
vBatchFileName,vUpdateName: string;
vProcessInfo: TProcessInformation;
vStartUpInfo: TStartupInfo;
begin
vBatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';
vUpdateName:=ParamStr(0)+'_Update.exe';
AssignFile(vBatchFile, vBatchFileName);
Rewrite(vBatchFile);
Writeln(vBatchFile, ':try');
Writeln(vBatchFile, 'Copy "'+ParamStr(0) + '" "'+vUpdateName+'"');
Writeln(vBatchFile, vUpdateName + ' /UPDATE');
Writeln(vBatchFile, 'del "' + vUpdateName + '"');
Writeln(vBatchFile,
'if exist "' + vUpdateName + '"' + ' goto try');
Writeln(vBatchFile, 'del %0');
CloseFile(vBatchFile);
FillChar(vStartUpInfo, SizeOf(vStartUpInfo), $00);
vStartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
vStartUpInfo.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(vBatchFileName), nil, nil,
False, IDLE_PRIORITY_CLASS, nil, nil, vStartUpInfo,
vProcessInfo) then
begin
CloseHandle(vProcessInfo.hThread);
CloseHandle(vProcessInfo.hProcess);
end;
end;

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/aroc_lo/archive/2008/09/20/2954836.aspx

相关阅读 >>

Delphi 文件路径结尾去掉“\”

Delphi 取时间戳

Delphi twebbrowser 用法

Delphi stringgrid之属性大全

Delphi urldownloadtofile 实现文件下载

Delphi 写3389自动登录器

Delphi xe 应用程序横竖屏设置

Delphi 如何通过代码控制打开键盘数字锁定numlock

Delphi 取得某一天所在的星期一及星期天

floattostr 将“浮点型”转换成“字符型”

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...