如何删除释放出来的资源文件,我用deletefile不行(100分)

  • 主题发起人 主题发起人 hxrhhy18
  • 开始时间 开始时间
H

hxrhhy18

Unregistered / Unconfirmed
GUEST, unregistred user!
[red]在delphi中,我把一个new.exe文件作为资源文件释放出来了,如果我手动关闭了exe文件或exe文件自动运行完毕,让nwe.exe文件自动从磁盘删除,应如何写代码, <br>前面的代码如下: <br><br>program Project2; <br><br>uses <br>windows, shellapi, classes; //必须用到的单元 <br><br><br>{$R exe.res} //<br>function ExtractRes(ResType, ResName, ResNewName: string): boolean; //var <br>Res: TResourceStream; <br>begin <br>try <br>Res := TResourceStream.Create(Hinstance, Resname, Pchar(ResType)); <br>try <br>Res.SavetoFile(ResNewName); <br>Result := true; <br>finally <br>Res.Free; <br>end; <br>except <br>Result := false; <br>end; <br>end; <br><br><br>var <br>handle:Thandle; //<br>begin <br>ExtractRes('exefile','mask','c:/new.exe'); //<br>ShellExecute(handle, 'open', PChar('c:/new.exe'), nil, nil, SW_SHOW); //调用shellexecuteend. <br>到这里为止,调用new.exe成功,我想在关闭new.exe运行窗口,或让他运行完毕,关闭窗口时,自动删除c:/new.exe,如何写代码,100分,请留下QQ,
 
如果new.exe也是你写的就好办了,在new中自己删除自己:<br>procedure SelfAutoDelete;<br>var Strs:TStrings;<br> &nbsp; &nbsp;Path:String;<br> &nbsp; &nbsp;FileName:String;<br>begin<br> &nbsp;Path:=ExtractFilePath(Application.ExeName);<br> &nbsp;FileName:=ExtractFileName(Application.ExeName);<br> &nbsp;Strs:=TStringList.Create;<br> &nbsp;try<br> &nbsp; &nbsp;Strs.Text:='@echo off'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +':Loop'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'del '+FileName+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'if exist '+FileName+' goto loop'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'del %0';<br> &nbsp; &nbsp;Strs.SaveToFile(Path+'DelUpdate.bat');<br> &nbsp;finally<br> &nbsp; &nbsp;FreeandNil(Strs);<br> &nbsp;end;<br> &nbsp;winexec(Pchar(Path+'DelUpdate.bat'), SW_HIDE);<br>end;<br><br>begin<br> &nbsp;Application.Initialize;<br> &nbsp;Application.CreateForm(TMainForm, MainForm);<br> &nbsp;.................<br> &nbsp;Application.Run;<br>//***************************************************************//<br> &nbsp;SelfAutoDelete;<br>//***************************************************************//<br>end.
 
//删除自已<br>procedure deletemyexe;<br>var<br>F:TextFile;<br>begin<br>AssignFile(F,'delself.bat');<br>Rewrite(F);{F?aTextFileààDí}<br>WriteLn(F,'del '+ExtractFileName(Paramstr(0)));<br>WriteLn(F,'del %0');<br>CloseFile(F);<br>WinExec('delself.bat',SW_HIDE);//<br>end;<br><br><br>调用deletemyexe;<br><br><br>WriteLn(F,'del '+ExtractFileName(Paramstr(0)));<br>这句如果你的EXE不是控制台程序就换成<br>WriteLn(F,'del '+ExtractFileName(Application.Exename));
 
只能删除自己吗?这个new.exe不是自己写的,难道就没有办法删除吗?请高手不吝出招,200分全给都没有问题.
 
改一下不就可以删除别人了:<br>procedure DeleteExeFile(ExeName:String);<br>var Strs:TStrings;<br> &nbsp; &nbsp;Path:String;<br> &nbsp; &nbsp;FileName:String;<br>begin<br> &nbsp;Path:=ExtractFilePath(ExeName);<br> &nbsp;FileName:=ExtractFileName(ExeName);<br> &nbsp;Strs:=TStringList.Create;<br> &nbsp;try<br> &nbsp; &nbsp;Strs.Text:='@echo off'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +':Loop'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'del '+FileName+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'if exist '+FileName+' goto loop'+#13#10<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'del %0';<br> &nbsp; &nbsp;Strs.SaveToFile(Path+'DelUpdate.bat');<br> &nbsp;finally<br> &nbsp; &nbsp;FreeandNil(Strs);<br> &nbsp;end;<br> &nbsp;winexec(Pchar(Path+'DelUpdate.bat'), SW_HIDE);<br>end;<br><br>begin<br> &nbsp;...........<br> &nbsp;DeleteExeFile('c:/new.exe');<br>end;
 
我試一下,等下聯繫
 
問題已經解決,謝謝大蝦
 
接受答案了.
 
后退
顶部