By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,938 Members | 2,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,938 IT Pros & Developers. It's quick & easy.

Deleting a file off a Drive, beyond kill

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Would something like this work if I just resaved over the file a couple
times? Or does this re-write to a different spot on the drive?

'Pass in file to delete path.....

dim WriteToFile(9) as string

for i as integer = 0 to 8

WriteToFile(i) = SecureDelete("c:\MyFileToBeWiped")

next

....blah blah blah...open and rewrite file.....Save....?

---------------------------------

Public Function SecureDelete() As String '(ByVal PassedPath As String)

Dim i As Integer

Dim DimFill As String

For i = 1 To 10021

DimFill += rand().ToString

Next

DimFill += DimFill + DimFill

Return DimFill

End Function

-------------------------------------------------------------

Public Function rand() As Integer

Dim obj As New System.Random(Now.Millisecond)

Return obj.Next(0, 2)

End Function
Nov 21 '05 #2

P: n/a
* pe***@mclinn.com (Peter) scripsit:
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?


Take a look at the 'System.IO.BinaryWriter' class. Notice that there is
no guarantee that the data is overwritten.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

P: n/a
Herfried,

Then would it be possible to find the exact location of the bytes on the
drive, and overwrite them?

-Peter

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O0**************@tk2msftngp13.phx.gbl...
* pe***@mclinn.com (Peter) scripsit:
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?


Take a look at the 'System.IO.BinaryWriter' class. Notice that there is
no guarantee that the data is overwritten.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4

P: n/a

"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
Nov 21 '05 #5

P: n/a
Is there nothing that can be done to secure win95-WinMe deletions?
Nov 21 '05 #6

P: n/a
I found this on Planet Source Code. It is writen in vb6. I take it
that the binary read in vb6 grabs the exact bytes and the binary
reader in vb.net doesn't? What do you think of this code?

Function DeleteFile(Path As String)
'This is an extremely quick file delete
' developed
'by me in about 5 minutes.
'overwrites the file 21 times then delet
' es it
'clean off your disk :-)
Dim i As Integer 'variable For times To overwrite
Dim Data1 As String, Data2 As String, Data3 As String, Data4 As
String, Data5 As String, Data6 As String, Data7 As String, Data8 As
String, Data9 As String, Data10 As String, Data11 As String, Data12 As
String, Data13 As String, Data14 As String, Data15 As String, Data16
As String, Data17 As String, Data18 As String, Data19 As String,
Data20 As String
'^^^ all 20 data variables, which hold t
' he information to overwrite the file wit
' h
Dim FinalByte As Byte 'just a byte To Do the final overwrite With
Data1 = Chr(85) 'the variables information
Data2 = Chr(170) 'the variables information
Data3 = Chr(74) 'the variables information
Data4 = Chr(99) 'the variables information
Data5 = Chr(71) 'the variables information
Data6 = Chr(92) 'the variables information
Data7 = Chr(101) 'the variables information
Data8 = Chr(112) 'the variables information
Data9 = Chr(1) 'the variables information
Data10 = Chr(61) 'the variables information
Data11 = Chr(97) 'the variables information
Data12 = Chr(119) 'the variables information
Data13 = Chr(86) 'the variables information
Data14 = Chr(79) 'the variables information
Data15 = Chr(109) 'the variables information
Data16 = Chr(72) 'the variables information
Data17 = Chr(90) 'the variables information
Data18 = Chr(0) 'the variables information
Data19 = Chr(255) 'the variables information
Data20 = Chr(212) 'the variables information
Open Path For Binary Access Write As #1 'open the path so we can
overwrite it
For i = 1 To 10 'a Loop
Put #1, , Data1 'overwrite
Next i 'stop Loop
For i = 1 To 10 'another Loop
Put #1, , Data2 'overwrite
Next i 'stop Loop
For i = 1 To 10 'another Loop
Put #1, , Data3 'overwrite
Next i 'stop Loop
For i = 1 To 10 'another Loop
Put #1, , Data4 'overwrite
Next i 'stop Loop
For i = 1 To 10 'another Loop
Put #1, , Data5 'overwrite
Next i 'stop Loop
For i = 1 To 10 'Im sure you Get the point from here on!
'that this is just the overwriting stage
' !
Put #1, , Data6
Next i
For i = 1 To 10
Put #1, , Data7
Next i
For i = 1 To 10
Put #1, , Data8
Next i
For i = 1 To 10
Put #1, , Data9
Next i
For i = 1 To 10
Put #1, , Data10
Next i
For i = 1 To 10
Put #1, , Data11
Next i
For i = 1 To 10
Put #1, , Data12
Next i
For i = 1 To 10
Put #1, , Data13
Next i
For i = 1 To 10
Put #1, , Data14
Next i
For i = 1 To 10
Put #1, , Data15
Next i
For i = 1 To 10
Put #1, , Data16
Next i
For i = 1 To 10
Put #1, , Data17
Next i
For i = 1 To 10
Put #1, , Data18
Next i
For i = 1 To 10
Put #1, , Data19
Next i
For i = 1 To 10
Put #1, , Data20
Next i
For i = 1 To 10 'the final Loop
Put #1, , FinalByte 'the final overwrite
Next i 'stop final Loop
Close #1 'close the file
Kill Path 'delete it
MsgBox "All Done Wiping The File!", vbInformation + vbOKOnly, "All
Done!" 'duh
End Function
Nov 21 '05 #7

P: n/a
Cancel that last order.
Nov 21 '05 #8

P: n/a
Cancel that last order.
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.