Home arrow Delphi-Kylix arrow Page 3 - Secure File Deletion in Delphi
DELPHI-KYLIX

Secure File Deletion in Delphi


Everybody knows that simply deleting a file from a hard disk doesn't truly make the file impossible to recover. Often, this is a good thing, but sometimes you want a file to be thoroughly deleted. Keep reading to find out how to accomplish this.

Author Info:
By: Jacques Noah
Rating: 4 stars4 stars4 stars4 stars4 stars / 12
November 14, 2005
TABLE OF CONTENTS:
  1. · Secure File Deletion in Delphi
  2. · Building the GUI
  3. · The code
  4. · The rest of the code

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Secure File Deletion in Delphi - The code
(Page 3 of 4 )

I’ve tried to comment as much as possible in the code. We will start writing the two procedures that actually power this app.

Add the following lines just above the private section:

procedure overwriteFile(FileName: string);
procedure getridofFile(const FileName: string);

The following two procedures will implement the algorithm as discussed before. Add the code in the implementation section of your form:

procedure TForm1.overwriteFile(FileName: string);
const
  Buffer       = 1024;
  Counttowrite = 34;
  FillBuffer: array[0..5] of Integer = ($00, $FF, $00, $F0, $0F, $00);
var
  arr: array[1..Buffer] of Byte;
  f: file;
  i, j, n: Integer;
begin
//open the file
  AssignFile(f, FileName);
  Reset(f, 1);
  n := FileSize(f);
//overwrite the file 34 times
  for j := 0 to Counttowrite do
  begin
    for i := 1 to n div Buffer do
    begin
      BlockWrite(f, FillBuffer[j], Buffer);
     form1.label1.caption:=inttostr(form1.prog.Position)+'%';
  end;
  form1.prog.position:=j+1;
  end;
  CloseFile(f);
  RenameFile(FileName, ExtractFilepath
(FileName) + '$000000.tmp');
  DeleteFile(ExtractFilepath(FileName) +
'$000000.tmp');
  end;

 procedure TForm1.getridofFile(const FileName:
string);
var
  newname: string;
begin
  // first rename the file
  newname := ExtractFilepath(FileName) +
'$000000.tmp';

  if not RenameFile(FileName, newname) then
    raise
    Exception.CreateFmt('Cannot find %s !',
[FileName]);

 overwriteFile(newname);

  DeleteFile(newname);

end;

The overwritefile procedure overwrites the contents of the file and then saves the file as ‘$000000.tmp' while updating the progressbar at the same time. The second procedure called getridofFile, grabs the file called ‘$000000.tmp', overwrites it again and then deletes it.

The two procedures are the meat of the application, and are also very simple to use and understand. Let’s move on to the rest of the procedures needed to use the app.


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials