怎么样删除应用程序自己? 我想在应用程序退出时删除自己,记得在哪见过的,现在找不到了,帮帮忙。100就是你的了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以创建一个BAT文件然后在关闭程序的时候调用它{BAT文件可以写: delete [你的文件名]} 猛料里复制的!我还没测试! procedure DeleteSelf; varmodule : HMODULE;buf : array[0..MAX_PATH - 1] of char;p : ULONG;hKrnl32 : HMODULE;pExitProcess, pDeleteFile, pFreeLibrary: pointer;beginmodule := GetModuleHandle(nil);GetModuleFileName(module, buf, sizeof(buf));CloseHandle(THandle(4));p := ULONG(module) + 1;hKrnl32 := GetModuleHandle('kernel32');pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');pDeleteFile := GetProcAddress(hKrnl32, 'DeleteFileA');pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');asmlea eax, bufpush 0push 0push eaxpush pExitProcesspush ppush pDeleteFilepush pFreeLibraryretend;end;把DeleteSelf代码放在DPR文件的最后一句即可,程序关闭之后,就会自动删除文件!此外,如果要任何时候都删除Exe的话,可以配合ExitProc&TerminateProc全局变量和AddExitProc&AddTerminateProc来做到这一点!!这个过程的优点在于不需要一个临时文件,保密程度和技术方面都比较强的。当然,你也可以利用一个临时的批处理文件来达到目的!procedure DeleteMe;varBatchFile: TextFile;BatchFileName: string;ProcessInfo: TProcessInformation;StartUpInfo: TStartupInfo;beginBatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';AssignFile(BatchFile, BatchFileName);Rewrite(BatchFile);Writeln(BatchFile, ':try');Writeln(BatchFile, 'del "' + ParamStr(0) + '"');Writeln(BatchFile,'if exist "' + ParamStr(0) + '"' + ' goto try');Writeln(BatchFile, 'del %0');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) thenbeginCloseHandle(ProcessInfo.hThread);CloseHandle(ProcessInfo.hProcess);end;end;************** Delphi编程技巧(作者:经乾 2000年04月13日 15:32)8.让应用程序自身删除 原理:在应用程序刚要退出之前创建一个Delself.bat文件,让它先删除应用程序,然后删除自身。在Form的OnClose事件中加入下列代码: AssignFile(F, ′delself.bat′);Rewrite(F);{F为TextFile类型} close WriteLn(F,′del ′+ExtractFileName(Application.ExeName)); WriteLn(F,′del %0′);CloseFile(F); WinExec(′delself.bat′,SW—HIDE); 《Delphi下深入Windows核心编程》中有详细的 例子。 这个是我主证实右用的//----------------------------unit Unit1;interfaceuses Windows, Messages, SysUtils, 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 DeleteMe;var BatchFile: TextFile; BatchFileName: string; ProcessInfo: TProcessInformation; StartUpInfo: TStartupInfo;begin BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat'; AssignFile(BatchFile, BatchFileName); Rewrite(BatchFile); Writeln(BatchFile, ':try'); Writeln(BatchFile, 'del "' + ParamStr(0) + '"'); Writeln(BatchFile, 'if exist "' + ParamStr(0) + '"' + ' goto try'); Writeln(BatchFile, 'del %0'); 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;end;procedure TForm1.Button1Click(Sender: TObject);begin DeleteMe; close;end;end.//--------------------------- 动态调用DLL出错,请问错在那里? Delphi程序员能拿多少工资? 问一个网络抓包的问题 大家请帮帮忙,如何将十六进制的字符串转化成十六进制的值 Delphi 7 IDE的那些面板不能像Delphi 2007一样停靠起来的么?非得悬浮的? 马上可给分:郁闷这应该定义成何数组?? 关于copy函数 能不能像建树一样建文件夹呀或是将建好的树导出成文件夹.(急.....) 怎样把DBGrid中的数据输出为MS Excel 如何用enter代替tab键??急!急!!急!!! SQL Server如何在运行的时候恢复数据库? 如何用ADO联结Paradox的数据表?
然后在关闭程序的时候调用它
{BAT文件可以写:
delete [你的文件名]}
procedure DeleteSelf;
varmodule : HMODULE;buf : array[0..MAX_PATH - 1] of char;p : ULONG;hKrnl32 : HMODULE;pExitProcess, pDeleteFile, pFreeLibrary: pointer;beginmodule := GetModuleHandle(nil);GetModuleFileName(module, buf, sizeof(buf));CloseHandle(THandle(4));p := ULONG(module) + 1;hKrnl32 := GetModuleHandle('kernel32');pExitProcess := GetProcAddress(hKrnl32, 'ExitProcess');pDeleteFile := GetProcAddress(hKrnl32, 'DeleteFileA');pFreeLibrary := GetProcAddress(hKrnl32, 'FreeLibrary');asmlea eax, bufpush 0push 0push eaxpush pExitProcesspush ppush pDeleteFilepush pFreeLibraryretend;end;把DeleteSelf代码放在DPR文件的最后一句即可,程序关闭之后,就会自动删除文件!此外,如果要任何时候都删除Exe的话,可以配合ExitProc&TerminateProc全局变量和AddExitProc&AddTerminateProc来做到这一点!!这个过程的优点在于不需要一个临时文件,保密程度和技术方面都比较强的。当然,你也可以利用一个临时的批处理文件来达到目的!procedure DeleteMe;varBatchFile: TextFile;BatchFileName: string;ProcessInfo: TProcessInformation;StartUpInfo: TStartupInfo;beginBatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';AssignFile(BatchFile, BatchFileName);Rewrite(BatchFile);Writeln(BatchFile, ':try');Writeln(BatchFile, 'del "' + ParamStr(0) + '"');Writeln(BatchFile,'if exist "' + ParamStr(0) + '"' + ' goto try');Writeln(BatchFile, 'del %0');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) thenbeginCloseHandle(ProcessInfo.hThread);CloseHandle(ProcessInfo.hProcess);end;end;**************
(作者:经乾 2000年04月13日 15:32)
8.让应用程序自身删除 原理:在应用程序刚要退出之前创建一个Delself.bat文件,让它先删除应用程序,然后删除自身。在Form的OnClose事件中加入下列代码: AssignFile(F, ′delself.bat′);Rewrite(F);{F为TextFile类型}
close
WriteLn(F,′del ′+ExtractFileName(Application.ExeName)); WriteLn(F,′del %0′);CloseFile(F); WinExec(′delself.bat′,SW—HIDE);
//----------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, 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 DeleteMe;
var
BatchFile: TextFile;
BatchFileName: string;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat'; AssignFile(BatchFile, BatchFileName);
Rewrite(BatchFile); Writeln(BatchFile, ':try');
Writeln(BatchFile, 'del "' + ParamStr(0) + '"');
Writeln(BatchFile,
'if exist "' + ParamStr(0) + '"' + ' goto try');
Writeln(BatchFile, 'del %0');
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;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
DeleteMe;
close;
end;end.
//---------------------------