Secure file deletion is a way of deleting a file, so that it cannot be recovered*. When you delete a file from the Recycle Bin, you'd assume that it's gone forever. This is incorrect. When you delete a file from the recycle bin, it just tells windows that the file can be over-written if windows needs the space. Anyobody can come along and recover it, easily using a tool such asRecuva by Piriform.
Secure file deletion over-writes a file before it deletes it from the recycle bin - so the original contents of the files are virtually impossible to recover. In this tutorial, I will show you how to securely delete a file in VB.NET.
*A message to all those who are pros at secure file deletion:
I know that if the FBI wants what's on your hard drive, and it's been securely deleted with a method like this, then they'll probably be able to recover some of it with hardware based tools. But to the average Joe, this will work fine.
Okey, so first we want to start up Visual Basic (I use Visual Basic 2010 Express Edition). Then we need to create a new Windows Forms Application. Call it whatever you want (I called it SecureDelete).
Now, Add a textbox, and two buttons to your form. Name the first button 'btnChoose', the second button 'btnDelete', and the textbox 'txtFilePath'.
Now, switch to the code view, and outside of any events, add this code:
Dim ofd As New OpenFileDialog
This code will create a new OpenFileDialog for us, so the user can choose which file to delete.
Next, go back to your form, and double click 'btnChoose'. Add this code in the Button_Click event:
'Make the title say "Choose File to Delete"
ofd.Title = "Choose File to Delete"
'Make sure that the user can choose any file
ofd.Filter = "All Files|*.*"
'Show the OpenFileDialog, and if the user presses OK,
'then set the text of the file path textbox to the path of the selected file.
If ofd.ShowDialog = DialogResult.OK Then
txtFilePath.Text = ofd.FileName
End If
Basically we just asked the user which file they wanted to delete, and then set the text in the textbox to the filepath of that file.
This is where stuff gets a little more complex:
Next, go back to your form, and double click 'btnDelete'. Add this code in the Button_Click event:
'First we need to check if the filepath in the textbox exists
If My.Computer.FileSystem.FileExists(txtFilePath.Text) Then
'Then we want to declare the number of times we want to over write the file:
Dim passes As Integer = 0
'We might encounter some errors in this section, such as "Access Denied", or something, so we need to use a Try...Catch statement.
Try
'In this case we're going to over-write the file ten times.
Do Until passes = 10
passes += 1
'Create a new Byte Array for all the bytes in the file
Dim array = IO.File.ReadAllBytes(txtFilePath.Text)
'A little messy, but this code will just replace each charactor in the array with "1" or "0".
Dim OneOrZero As Integer = 0
Dim all As String = String.Empty
'For each byte in the array...
For Each selbyte In array
'Change it to one or zero...
If OneOrZero = 0 Then
selbyte = "0"
OneOrZero = 1
ElseIf OneOrZero = 1 Then
selbyte = "1"
OneOrZero = 0
End If
all &= selbyte
Next
'Write the new contents of the file to the file
Dim SaveF As New IO.StreamWriter(txtFilePath.Text)
SaveF.WriteLine(all)
SaveF.Close()
'Loop through untill we have completed the procedure 10 times...
Loop
'Finally delete the file.
My.Computer.FileSystem.DeleteFile(txtFilePath.Text, False, False)
Catch ex As Exception
'If we encounter any errors in the process, show a dialog
MessageBox.Show("Error: " & ex.Message, "Error")
End Try
Else
'If the file does not exist, then show a dialog.
MessageBox.Show("Error: the file specified does not exist. Please check the file name and try again", "Error")
End If
So what did we do there?
Basically, firstly, we checked to see if the filepath in the textbox existed. Then if it did, we created a byte array with all the data in the file in it. Then we replaced all of that with ones and zeroes, and saved that to the file. We repeated this procedure ten times.
Notes & Possible errors:
If the file which you are over-writing is large, then the program may not respond for a variable amount of time, depending upon how large the file is. This can be circumvented by using a backgroundworker.
This file deletion method is very basic, but it's the basis for a program.
Thanks for reading my tutorial - I hope it helped. There's lots more on web browsers coming whenever I have time to write about it!






MultiQuote





|