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 windows xp下屏蔽ctrl_alt_del键的方法

Delphi 扫二维码并识别

Delphi 如何禁止用户更改窗体的大小

Delphi 多重循环

cnpack sqlite tool 2.04

Delphi中tframe组件的使用

Delphi 读取图像文件base64编码加载到image组件显示图片

Delphi ascii 码对照表

Delphi image 等比例缩小

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



打赏

取消

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

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

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

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

评论

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