可执行文件运行时如何删除自己? 可执行文件运行时如何删除自己? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 例子procedure TForm1.DeleteMe(mDeleteDir: Boolean);var vExeDir: string; procedure pDelDir(mDirName: string); { ?除指定路? } var vSearchRec: TSearchRec; PathName: string; K: Integer; begin PathName := mDirName + '\*.*'; K := FindFirst(PathName, faAnyFile, vSearchRec); while K = 0 do begin if (vSearchRec.Attr and faDirectory > 0) and (Pos(vSearchRec.Name, '..') = 0) then begin {$WARNINGS OFF} FileSetAttr(vSearchRec.Name, faDirectory); {$WARNINGS ON} pDelDir(mDirName + '\' + vSearchRec.Name); end else if (Pos(vSearchRec.Name, '..') = 0) and (CompareText(mDirName + '\' + vSearchRec.Name, ParamStr(0)) <> 0) then begin {$WARNINGS OFF} FileSetAttr(vSearchRec.Name, 0); {$WARNINGS ON} DeleteFile(PChar(mDirName + '\' + vSearchRec.Name)); end; K := FindNext(vSearchRec); end; if CompareText(vExeDir, mDirName) <> 0 then RmDir(mDirName); end; { pDelDir }var BatchFile: TextFile; BatchFileName: TFileName; ProcessInfo: TProcessInformation; StartUpInfo: TStartupInfo;begin vExeDir := ExtractFileDir(ParamStr(0)); if mDeleteDir then pDelDir(vExeDir); 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; { DeleteMe }或则你可以自己生成批处理文件,再用winExec , ShellExecute直接运行批处理文件 谢谢!请问什么地方可以找到批处理中类似“:try”、“'if exist ”等的用法说明 google echo、@、call、pause、rem 是批处理文件最常用的几个命令,我们就从他们开始学起。 echo 表示显示此命令后的字符 echo off 表示在此语句后所有运行的命令都不显示命令行本身 @ 与echo off相象,但它是加在其它命令行的最前面,表示运行时不显示命令行本身。 call 调用另一条批处理文件(如果直接调用别的批处理文件 ,执行完那条文件后将无法执行当前文件后续命令) pause 运行此句会暂停,显示Press any key to continue... 等待用户按任意键后继续 rem 表示此命令后的字符为解释行,不执行,只是给自己今后查找用的 例:用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat,执行该批处理文件后可实现:将根目录中所有文件写入 a.txt中,启动UCDOS,进入WPS等功能。 批处理文件的内容为: 文件表示: echo off 不显示命令行 dir c:\*.* >a.txt 将c盘文件列表写入a.txt call c:\ucdos\ucdos.bat 调用ucdos echo 你好 显示"你好" pause 暂停,等待按键继续 rem 使用wps 注释将使用wps cd ucdos 进入ucdos目录 wps 使用wps 批处理文件中还可以像C语言一样使用参数,这只需用到一个参数表示符%。 %表示参数,参数是指在运行批处理文件时在文件名后加的字符串。变量可以从 %0到%9,%0表示文件名本身,字符串用%1到%9顺序表示。 例如,C:根目录下一批处理文件名为f.bat,内容为 format %1 则如果执行C:\>f a: 则实际执行的是format a: 又如C:根目录下一批处理文件的名为t.bat,内容为 type %1 type %2 那么运行C:\>t a.txt b.txt 将顺序地显示a.txt和b.txt文件的内容 if goto choice for 是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。 if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式: 1、if "参数" == "字符串" 待执行的命令 参数如果等于指定的字符串,则条件成立,运行命令,否则运行下一句。(注意是两个等号) 如if "%1"=="a" format a: 2、if exist 文件名 待执行的命令 如果有指定的文件,则条件成立,运行命令,否则运行下一句。如if exist config.sys edit config.sys 3、if errorlevel 数字 待执行的命令 如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。如if errorlevel 2 goto x2 DOS程序运行时都会返回一个数字给DOS,称为错误码errorlevel或称返回码 goto 批处理文件运行到这里将跳到goto 所指定的标号处, 一般与if配合使用。 如: goto end :end echo this is the end 标号用 :字符串 表示,标号所在行不被执行 choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。使用时应该加/c:参数,c:后应写提示可输入的字符,之间无空格。它的返回码为1234…… 如: choice /c:dme defrag,mem,end 将显示 defrag,mem,end[D,M,E]? 例如,test.bat的内容如下: @echo off choice /c:dme defrag,mem,end if errorlevel 3 goto defrag 应先判断数值最高的错误码 if errorlevel 2 goto mem if errotlevel 1 goto end :defrag c:\dos\defrag goto end :mem mem goto end :end echo good bye 此文件运行后,将显示 defrag,mem,end[D,M,E]? 用户可选择d m e ,然后if语句将作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,文件结束。 for 循环命令,只要条件符合,它将多次执行同一命令。 格式FOR [%%f] in (集合) DO [命令] 只要参数f在指定的集合内,则条件成立,执行命令 如果一条批处理文件中有一行: for %%c in (*.bat *.txt) do type %%c 含义是如果是以bat或txt结尾的文件,则显示文件的内容。 autoexec.bat DOS在启动会自动运行autoexec.bat这条文件,一般我们在里面装载每次必用的程序,如: path(设置路径)、smartdrv(磁盘加速)、 mouse(鼠标启动)、mscdex(光驱连接)、 doskey(键盘管理)、set(设置环境变量)等。 如果启动盘根目录中没有这个文件,电脑会让用户输入日期和时间。 例如,一个典型的autoexec.bat内容如下: @echo off 不显示命令行 prompt $p$g 设置提示符前有目录提示 path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools 设置路径 lh c:\dos\doskey.com 加载键盘管理 lh c:\mouse\mouse.com 加载鼠标管理 lh c:\dos\smartdrv.exe 加载磁盘加速管理 lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V 加载CD-ROM驱动 set temp=c:\temp 设置临时目录 这个更简单一些:procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);var F: TextFile;begin AssignFile(F,'delself.bat'); Rewrite(F);{F为TextFile类型} WriteLn(F,'del '+ExtractFileName(Application.ExeName)); WriteLn(F,'del %0'); CloseFile(F); WinExec('delself.bat',SW_HIDE);end; 用下面这段代码可以。procedure DeleteSelf;var module : HMODULE; buf : array[0..MAX_PATH - 1] of char; p : ULONG; hKrnl32 : HMODULE; pExitProcess, pDeleteFile, pFreeLibrary: pointer;begin module := 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'); asm lea eax, buf push 0 push 0 push eax push pExitProcess push p push pDeleteFile push pFreeLibrary ret end;end; dbchart中Bar显示问题 关于SQLTable的问题 超市小票打印是怎么实现 关于TWebBrowser和网页缓存问题!!知道的来帮帮忙啊。 请问:将Tcombobox的style属性设为csDropDownList后,就不能再赋值了,为什么? 请教:如何禁止局域网中某些计算机上网? 征集源码例子(三) 如何在Win2000下截取具有密码属性的TEdit控件中的内容? 关于delphi与网页的连接 我自学DELPHI已经有一年了,但..... rave 报表的问题,为什么固定报表有时能打印出来,有时某一列打印不出来 ? 只有30分了,请帮忙一下.
procedure TForm1.DeleteMe(mDeleteDir: Boolean);
var
vExeDir: string; procedure pDelDir(mDirName: string); { ?除指定路? }
var
vSearchRec: TSearchRec;
PathName: string;
K: Integer;
begin
PathName := mDirName + '\*.*';
K := FindFirst(PathName, faAnyFile, vSearchRec);
while K = 0 do begin
if (vSearchRec.Attr and faDirectory > 0) and
(Pos(vSearchRec.Name, '..') = 0) then begin
{$WARNINGS OFF}
FileSetAttr(vSearchRec.Name, faDirectory);
{$WARNINGS ON}
pDelDir(mDirName + '\' + vSearchRec.Name);
end else if (Pos(vSearchRec.Name, '..') = 0) and
(CompareText(mDirName + '\' + vSearchRec.Name, ParamStr(0)) <> 0) then begin
{$WARNINGS OFF}
FileSetAttr(vSearchRec.Name, 0);
{$WARNINGS ON}
DeleteFile(PChar(mDirName + '\' + vSearchRec.Name));
end;
K := FindNext(vSearchRec);
end;
if CompareText(vExeDir, mDirName) <> 0 then RmDir(mDirName);
end; { pDelDir }var
BatchFile: TextFile;
BatchFileName: TFileName;
ProcessInfo: TProcessInformation;
StartUpInfo: TStartupInfo;
begin
vExeDir := ExtractFileDir(ParamStr(0));
if mDeleteDir then pDelDir(vExeDir);
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; { DeleteMe }或则你可以自己生成批处理文件,再用winExec , ShellExecute直接运行批处理文件
请问什么地方可以找到批处理中类似“:try”、“'if exist ”等的用法说明
echo off 表示在此语句后所有运行的命令都不显示命令行本身
@ 与echo off相象,但它是加在其它命令行的最前面,表示运行时不显示命令行本身。
call 调用另一条批处理文件(如果直接调用别的批处理文件 ,执行完那条文件后将无法执行当前文件后续命令)
pause 运行此句会暂停,显示Press any key to continue... 等待用户按任意键后继续
rem 表示此命令后的字符为解释行,不执行,只是给自己今后查找用的
例:用edit编辑a.bat文件,输入下列内容后存盘为c:\a.bat,执行该批处理文件后可实现:将根目录中所有文件写入 a.txt中,启动UCDOS,进入WPS等功能。 批处理文件的内容为: 文件表示: echo off 不显示命令行 dir c:\*.* >a.txt 将c盘文件列表写入a.txt call c:\ucdos\ucdos.bat 调用ucdos echo 你好 显示"你好" pause 暂停,等待按键继续 rem 使用wps 注释将使用wps cd ucdos 进入ucdos目录 wps 使用wps 批处理文件中还可以像C语言一样使用参数,这只需用到一个参数表示符%。 %表示参数,参数是指在运行批处理文件时在文件名后加的字符串。变量可以从 %0到%9,%0表示文件名本身,字符串用%1到%9顺序表示。 例如,C:根目录下一批处理文件名为f.bat,内容为 format %1 则如果执行C:\>f a: 则实际执行的是format a: 又如C:根目录下一批处理文件的名为t.bat,内容为 type %1 type %2 那么运行C:\>t a.txt b.txt 将顺序地显示a.txt和b.txt文件的内容 if goto choice for 是批处理文件中比较高级的命令,如果这几个你用得很熟练,你就是批处理文件的专家啦。 if 表示将判断是否符合规定的条件,从而决定执行不同的命令。 有三种格式:
1、if "参数" == "字符串" 待执行的命令
参数如果等于指定的字符串,则条件成立,运行命令,否则运行下一句。(注意是两个等号)
如if "%1"=="a" format a: 2、if exist 文件名 待执行的命令
如果有指定的文件,则条件成立,运行命令,否则运行下一句。如if exist config.sys edit config.sys 3、if errorlevel 数字 待执行的命令
如果返回码等于指定的数字,则条件成立,运行命令,否则运行下一句。如if errorlevel 2 goto x2 DOS程序运行时都会返回一个数字给DOS,称为错误码errorlevel或称返回码 goto 批处理文件运行到这里将跳到goto 所指定的标号处, 一般与if配合使用。 如: goto end :end
echo this is the end 标号用 :字符串 表示,标号所在行不被执行 choice 使用此命令可以让用户输入一个字符,从而运行不同的命令。使用时应该加/c:参数,c:后应写提示可输入的字符,之间无空格。它的返回码为1234…… 如: choice /c:dme defrag,mem,end
将显示
defrag,mem,end[D,M,E]? 例如,test.bat的内容如下:
@echo off
choice /c:dme defrag,mem,end
if errorlevel 3 goto defrag 应先判断数值最高的错误码
if errorlevel 2 goto mem
if errotlevel 1 goto end :defrag
c:\dos\defrag
goto end :mem
mem
goto end :end
echo good bye 此文件运行后,将显示 defrag,mem,end[D,M,E]? 用户可选择d m e ,然后if语句将作出判断,d表示执行标号为defrag的程序段,m表示执行标号为mem的程序段,e表示执行标号为end的程序段,每个程序段最后都以goto end将程序跳到end标号处,然后程序将显示good bye,文件结束。 for 循环命令,只要条件符合,它将多次执行同一命令。 格式FOR [%%f] in (集合) DO [命令]
只要参数f在指定的集合内,则条件成立,执行命令 如果一条批处理文件中有一行:
for %%c in (*.bat *.txt) do type %%c
含义是如果是以bat或txt结尾的文件,则显示文件的内容。 autoexec.bat DOS在启动会自动运行autoexec.bat这条文件,一般我们在里面装载每次必用的程序,如: path(设置路径)、smartdrv(磁盘加速)、 mouse(鼠标启动)、mscdex(光驱连接)、 doskey(键盘管理)、set(设置环境变量)等。 如果启动盘根目录中没有这个文件,电脑会让用户输入日期和时间。 例如,一个典型的autoexec.bat内容如下: @echo off 不显示命令行 prompt $p$g 设置提示符前有目录提示 path c:\dos;c:\;c:\windows;c:\ucdos;c:\tools 设置路径 lh c:\dos\doskey.com 加载键盘管理 lh c:\mouse\mouse.com 加载鼠标管理 lh c:\dos\smartdrv.exe 加载磁盘加速管理 lh c:\dos\mscdex /S /D:MSCD000 /M:12 /V 加载CD-ROM驱动 set temp=c:\temp 设置临时目录
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
F: TextFile;
begin
AssignFile(F,'delself.bat');
Rewrite(F);{F为TextFile类型}
WriteLn(F,'del '+ExtractFileName(Application.ExeName));
WriteLn(F,'del %0');
CloseFile(F);
WinExec('delself.bat',SW_HIDE);
end;
var
module : HMODULE;
buf : array[0..MAX_PATH - 1] of char;
p : ULONG;
hKrnl32 : HMODULE;
pExitProcess, pDeleteFile, pFreeLibrary: pointer;
begin
module := 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');
asm
lea eax, buf
push 0
push 0
push eax
push pExitProcess
push p
push pDeleteFile
push pFreeLibrary
ret
end;
end;