程序打开另一个EXE文件,打开后删除原EXE文件
解决方案 »
- Delphi 连接 Paradox 离开Delphi 环境后不能运行
- 如何引用Dll中的函数?
- 庆祝自己生日,放分!
- 如何把delphi程序打包
- 我的Delphi建立一个什么改动都没有的Application时运行也会出错,为什么!!!!
- 菜鸟问题 ,如何查询?来者有分
- 急!答对者,百分全送,关于dblookupcombbox的用法,, 已经设置了listsource.listfield.keyfield,属性, 为什么我的listsource所指的表的记录
- delphi vs oracle error
- List index out of bounds(0)这是什么错误啊?
- 与SQL SERVER 长时间连结会出现变慢或死机,如何解决?
- delphi6编写的服务程序常常死掉,除了写个监听服务,有没有好的办法
- dxDBGrid 如何录入数据
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
uses
ShellAPI,
ShlObj;
{ 自杀 - 退出程序并删除自身. }
function DeleteSelf: Boolean;
var
shi: SHELLEXECUTEINFO;
pcModule: array [0..MAX_PATH] of Char;
pcComspec: array [0..MAX_PATH] of Char;
strParams: string;
begin
Result := False; // 获取执行路径
if (GetModuleFileName(0, pcModule, MAX_PATH) <> 0)
and (GetShortPathName(pcModule, pcModule, MAX_PATH) <> 0)
and (GetEnvironmentVariable('COMSPEC', pcComspec, MAX_PATH) <> 0) then
begin
strParams := Format('/c del /f /q "%s" > nul', [pcModule]); shi.cbSize := SizeOf(shi);
shi.Wnd := 0;
shi.lpVerb := 'open';
shi.lpFile := pcComspec;
shi.lpParameters := PChar(strParams);
shi.lpDirectory := nil;
shi.nShow := SW_HIDE;
shi.fMask := SEE_MASK_NOCLOSEPROCESS; // 调整程序进程和线程的优先级
if not SetPriorityClass(GetCurrentProcess, REALTIME_PRIORITY_CLASS)
or not SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_TIME_CRITICAL) then
begin
Exit;
end; // 执行删除
if ShellExecuteEx(@shi) then
begin
// 先通知程序退出
PostQuitMessage(0); // 阻断 Command Shell 进程直到本程序退出
SetPriorityClass(shi.hProcess, IDLE_PRIORITY_CLASS);
SetProcessPriorityBoost(shi.hProcess, TRUE); // 向 Explorer Shell 通知删除消息
SHChangeNotify(SHCNE_DELETE, SHCNF_PATH, @pcModule, nil); Result := True;
end
else
begin
// 如果删除失败则恢复程序进程和线程的优先级
SetPriorityClass(GetCurrentProcess, NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread, THREAD_PRIORITY_NORMAL);
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//DeleteSelf;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
DeleteSelf;
end;end.
我以前收藏的,你试试吧
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
Selfname,BatFilename,s1,s2:string;
BatchFile: TextFile;
begin
Selfname:=Extractfilename(application.exename);//取EXE文件自己的名称
BatFilename:=ExtractFilePath(Application.ExeName)+ 'a.bat';//批处理文件名称
S1:='@del '+Selfname;
S2:='if exist '+Selfname+' goto pp';
assignfile(BatchFile,BatFilename);
rewrite(BatchFile);
writeln(BatchFile,':pp');
writeln(BatchFile,S1);
writeln(BatchFile,S2);
writeln(BatchFile,'@del %0');
closefile(BatchFile);
winexec(pchar(BatFilename),sw_hide);//隐藏窗口运行a.bat
application.Terminate;//退出程序
end;end.这个是用批处理实现的
其实方法很多 ,不过,这两个,楼主应该够了吧
procedure KillSelf;
var
f:textfile;
begin
assignfile(f,'.\killme.bat'); //在程序目录下新建一个bat文件
rewrite(f); //开始向这个bat文件写入内容
writeln(f,'@echo off');
writeln(f,':loop');
writeln(f,'del ""'+application.ExeName+'""');
writeln(f,'if exist .\file.exe goto loop');
writeln(f,'del .\killme.bat');
closefile(f); //写入内容结束
winexec('.\killme.bat', SW_HIDE); //调用删除自己bat文件,
close; //关闭自己
end;用批处理可以