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 datasnap传输流/文件问题

Delphi windows服务控制单元

Delphi listview高速添加数据

Delphi用idtcpserver和idtcpclient传输文件

Delphi显示 jpg、png、gif 图片及 gif 动画

字符串提取算法

Delphi 正则提取txt中的email 数据

正则表达式初学入门

Delphi 窗体form的formstyle属性设置为fsstayontop时属性设置不起作用问题探讨。

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



打赏

取消

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

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

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

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

评论

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