我想在应用程序退出时删除自己,记得在哪见过的,现在找不到了,帮帮忙。
100就是你的了

解决方案 »

  1.   

    可以创建一个BAT文件
    然后在关闭程序的时候调用它
    {BAT文件可以写:
     delete [你的文件名]}
      

  2.   

    猛料里复制的!我还没测试! 
    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;************** 
      

  3.   

    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);  
      

  4.   

    《Delphi下深入Windows核心编程》中有详细的 例子。
      

  5.   

    这个是我主证实右用的
    //----------------------------
    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.
    //---------------------------