"Peter" <pe***@mclinn.com> wrote in message
news:dc*************************@posting.google.co m...
I'm trying to write a proceedure that will delete a file completely
off a hardrive. I just don't want to kill the file. I want to
rewrite over the data with 1's and 0's 8 times. Anyone have an idea
or sample source?
-Peter
Copying over the file almost certianly won't work, as the new file will be
created somewhere different, and then the directory updated. Otherwise if
the copy failed half way through, the old file would be lost.
Here's a sample which opens the file, writes zeros over its contents and
closes it multiple times. It's important to close the file, and important
to use certian parameters when opening the file.
The tricky thing is that if write caching is turned on for the disk then all
of your writing may really be to memory and bits may only be written to disk
once. To try and guarantee that the phisical disk is overwritten each time
we have to open the file with the FILE_FLAG_WRITE_THROUGH attribute, which
means, in turn, that we have to open the file with the Win32 CreateFile API.
Anyway here's a sample which should work on local NTFS volumes. There's no
way to tell how other types of storage would work. For instance a USP flash
device. We don't know what's actually written where. And anyway without
intimate knoledge of the storage hardware, this is about all we can do.
David
Imports System.IO
Imports System.Runtime.InteropServices
Public Class EraseFile
Shared ones(1024 * 8) As Byte
Shared zeros(1024 * 8) As Byte
Shared Sub New()
For i As Integer = 0 To ones.Length - 1
ones(i) = 1
Next
End Sub
Private Declare Function CreateFile _
Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
<MarshalAs(UnmanagedType.Struct)> _
ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As Integer) As Integer
<StructLayout(LayoutKind.Sequential)> _
Private Structure SECURITY_ATTRIBUTES
Public nLength As Integer
Public lpSecurityDescriptor As Integer
Public bInheritHandle As Integer
End Structure
Public Const GENERIC_WRITE = &H40000000
Public Const OPEN_ALWAYS = 4
Public Const FILE_FLAG_WRITE_THROUGH = &H80000000
Private Shared Function _
OpenFileWriteThrough(ByVal FileName As String) As FileStream
Dim fh As Integer = CreateFile(FileName, _
GENERIC_WRITE, _
0, _
Nothing, _
OPEN_ALWAYS, _
FILE_FLAG_WRITE_THROUGH, _
0)
Return New FileStream(New IntPtr(fh), FileAccess.Write)
End Function
Public Shared Sub EraseFile(ByVal FileName As String)
For i As Integer = 0 To 8
Dim f As FileStream
f = OpenFileWriteThrough(FileName)
Dim flen As Long = f.Length
f.Position = 0
Do While f.Position < flen
f.Write(ones, 0, Math.Min(ones.Length, flen - f.Position))
Loop
f.Close()
f = OpenFileWriteThrough(FileName)
f.Position = 0
Do While f.Position < flen
f.Write(zeros, 0, Math.Min(ones.Length, flen - f.Position))
Loop
f.Close()
Next
File.Delete(FileName)
End Sub
Public Shared Sub Main(ByVal args() As String)
EraseFile("d:\victim.dat")
End Sub
End Class