首页  编辑  

彻底清除文件

Tags: /超级猛料/Stream.File.流、文件和目录/文件操作/   Date Created:

Wipe File?

procedure WipeFile(FileName: string);

var

 buffer: array [0..4095] of Byte;

 max, n: LongInt;

 i: Integer;

 fs: TFileStream;

 procedure RandomizeBuffer;

 var

   i: Integer;

 begin

   for i := Low(buffer) to High(buffer) do

     buffer[i] := Random(256);

 end;

begin

 fs := TFilestream.Create(FileName, fmOpenReadWrite or fmShareExclusive);

 try

   for i := 1 to 3 do  

   begin

     RandomizeBuffer;

     max         := fs.Size;

     fs.Position := 0;

     while max  0 do  

     begin

       if max  SizeOf(buffer) then

         n := SizeOf(buffer)

       else

         n := max;

       fs.Write(Buffer, n);

       max := max - n;

     end;

     FlushFileBuffers(fs.Handle);

   end;

 finally

   fs.Free;

 end;

 Deletefile(FileName);

end;