473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deleting a file off a Drive, beyond kill

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
8 2680
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
* 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
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

"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
Is there nothing that can be done to secure win95-WinMe deletions?
Nov 21 '05 #6
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
Cancel that last order.
Nov 21 '05 #8
Cancel that last order.
Nov 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hal Vaughan | last post by:
I've seen install programs that search a hard drive for previous instances of a program, or to find installs of other needed programs. I need to search a hard drive for any installations of...
3
by: David B | last post by:
I have this code to find a file, place it in C:\ temp, zip it up then send it to floppy in drive A:\. When I run it it comes up with error 53 - source file not found. However it gets as far as...
3
by: Mike Turco | last post by:
I'm working on an application that imports and exports tons of CSV data, like 64,000 records per file, and six or seven files in a set. First off, by the tine I import a few hundred thousand...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
0
by: plucier | last post by:
Hey everybody. I'm not a programmer myself, but I'm working with a group of programmers and we've run into a problem. We have a situation where we need to delete files on a network drive. All...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
1
by: dkultasev | last post by:
Hello, I developed the program for multi use. The program reads data from the text file and puts modified data back to another file. This program was uploaded to mapped network drive. When I try...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.