一个纯粹的可执行程序,没有用到其他任何程序或者文件,怎样能实现自己升级自己?(在线),给出实现方案即可。
解决方案 »
- delphi+access 在一个表中想把一个字段改为关键字,并且设置默认值为-1
- 转16进制(送分题)
- 如何通过DBGRID更改ADOQUREY查询值
- 散分,三天后揭贴:)
- 怎样可以不在窗体中放置TMainMenu组件,而直接用代搞个TMainMenu出来呢?
- 急急急!是不是在delphi中的uodate语句不支持from ,where 子句?
- 高分征解,如何在两个ADOQuery之间快速的复制数据(两个数据集的结构完全相同)!!!
- 寻找专业的图形和建模Delphi团队
- 请教控件问题,羡慕啊!
- 关于MTS的问题,另外,谢谢launch(飞星子)关于动态打开窗口的答复
- 这个世界还是好人多
- 简单问题20分WinExec怎么使用啊?
如果不是win98系统,不用另外调用exe程序,只需在运行时改名即可。
然后把新下载的exe改成对应的名字,并删掉原来的exe文件
例:
主升级程序名:Update.exe
副:autoupdate.exe //此文件用于放到主升级程序中的资源中关键是副程序的代码,如下这段就是我用的
Autoupdate.dprprogram AutoUpdate;uses
Forms, windows, SysUtils;procedure DelMe; //程序自杀
function GetShortName(sLongName: string): string;
var //转换长文件名为短文件名
sShortName: string;
nShortNameLen: integer;
begin
SetLength(sShortName, 260);
nShortNameLen := GetShortPathName(pchar(sLongName), pchar(sShortName), 260 - 1);
if (0 = nShortNameLen) then
begin
// handle errors...
end;
SetLength(sShortName, nShortNameLen);
result := sShortName;
end;
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ExtractFilePath(ParamStr(0)) + '$$a$$.bat';
AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile);
Writeln(BatchFile, ':try');
Writeln(BatchFile, 'del "' + GetShortName(ParamStr(0)) + '"');
Writeln(BatchFile, 'if exist "' + GetShortName(ParamStr(0)) + '"' + ' goto try');
Writeln(BatchFile, 'del %0');
Writeln(BatchFile, 'cls');
Writeln(BatchFile, 'exit');
CloseFile(BatchFile);
FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);
StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_Hide;
if CreateProcess(nil, pchar(BatchFileName), nil, nil, false, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then
begin
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
Application.Terminate;
end;var AppPath: string;
begin
if ParamStr(1) <> '' then
begin
AppPath := ExtractFilePath(ParamStr(0));
Sleep(3000);//停3秒,有效让主升级程序退出
if FileExists(AppPath + 'Update\' + ParamStr(1)) then //查看是否有新的升级文件
begin
CopyFile(pchar(AppPath + 'Update\' + ParamStr(1)), pchar(AppPath + ParamStr(2)), false);//执行更新
DeleteFile(pchar(AppPath + 'Update\' + ParamStr(1)));//删除升级目录的文件
RemoveDirectory(pchar(ExtractFilePath(AppPath + 'Update\' + ParamStr(1)))); //把升级目录移除
end;
if FileExists(AppPath + ParamStr(2)) then WinExec(pchar(AppPath + ParamStr(2)), sw_show); //启动主升级程序
end;
DelMe;//删除自已
end.
编译以代码得到EXE,加到主程序update的资源里主程序检测版本如有新版本则释放资源中的autoupdate.exe到本目录
然后执行
ExtractRes('AUTOUPDATE', 'AUTOUPDATE', AppPath + 'AutoUpdate.exe');//释放资源
//参数方法
副程序名,参数1:下载回来的主升级EXE,参数2:当前主升级程序名路径
autoupdate.exe update\update.exe update.exe
ShellExecute(handle, 'open', pchar(AppPath + 'AutoUpdate.exe'), pchar(TmpFile + ' ' + ExtractFileName(ParamStr(0))), pchar(AppPath), sw_hide);//执行application.Terminate;//结束主程序
在被升级的A.EXE里面,封装另外一个B.EXE资源和D.BAT,对自我升级时,下载最新的A.EXE,存贮到本地改名为C.EXE,同时,A.EXE 释放B.EXE和D.BAT到本地,下载成功后,A.EXE(ONClOSE Event) 调用 C.EXE,C.EXE调用D.BAT删除A.EXE,同时更名C.EXE为A.EXE。OK!