One possibility would be to add each temporary file to the list of files that were deleted during system startup.
On the Windows NT platform (starting with Windows 2000), you can simply call the MoveFileEx function with the second parameter (destination) set to zero and with the MOVEFILE_DELAY_UNTIL_REBOOT flag.
On Windows 9x, this is much more complicated. You must edit the% WINDIR% \ wininit.ini file and write the entry to the [Rename] section.
MSDN Record How to move files that are currently in use ... describes both methods.
The DSiMoveOnReboot function (part of the free DSiWin32 ) handles both operating systems. If you pass an empty string as the second parameter, it will delete the original file upon reboot.
function DSiMoveOnReboot(const srcName, destName: string): boolean; var wfile: string; winit: text; wline: string; cont : TStringList; i : integer; found: boolean; dest : PChar; begin if destName = '' then dest := nil else dest := PChar(destName); if DSiIsWinNT then Result := MoveFileEx(PChar(srcName), dest, MOVEFILE_DELAY_UNTIL_REBOOT) else Result := false; if not Result then begin // not NT, write a Rename entry to WININIT.INI wfile := DSiGetWindowsFolder+'\wininit.ini'; if FileOpenSafe(wfile,winit,500,120{one minute}) then begin try cont := TStringList.Create; try Reset(winit); while not Eof(winit) do begin Readln(winit,wline); cont.Add(wline); end; //while if destName = '' then wline := 'NUL='+srcName else wline := destName+'='+srcName; found := false; for i := 0 to cont.Count - 1 do begin if UpperCase(cont[i]) = '[RENAME]' then begin cont.Insert(i+1,wline); found := true; break; end; end; //for if not found then begin cont.Add('[Rename]'); cont.Add(wline); end; Rewrite(winit); for i := 0 to cont.Count - 1 do Writeln(winit,cont[i]); Result := true; finally cont.Free; end; finally Close(winit); end; end; end; end; { DSiMoveOnReboot }
source share