美文网首页
2019-04-23 delphi 自删除

2019-04-23 delphi 自删除

作者: netppp | 来源:发表于2019-04-23 11:03 被阅读0次

    unit Unit1;

    interface

    uses

      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 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.

    wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

    unit Unit1;

    interface

    uses

      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}

    function WinExec(lpCmdline: PAnsiChar; uCmdShow: LongWord): LongWord;

                    stdcall; external 'kernel32.dll' name 'WinExec';

    function ExtractFilePath(FileName: string): string;

    begin

      Result := '';

      while ((Pos('/', FileName) <> 0) or (Pos('/', FileName) <> 0)) do

      begin

        Result := Result + Copy(FileName, 1, 1);

        Delete(FileName, 1, 1);

      end;

    end;

    procedure DeleteMe;

    var

      BatchFile: TextFile;

      BatchFileName: string;

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

    end;

    procedure TForm1.Button1Click(Sender: TObject);

    begin

      DeleteMe ;

      WinExec('_deleteme.bat',SW_HIDE);

    close;

    end;

    end.

    相关文章

      网友评论

          本文标题:2019-04-23 delphi 自删除

          本文链接:https://www.haomeiwen.com/subject/xzgmgqtx.html