473,659 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2713
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 :\MyFileToBeWip ed")

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(N ow.Millisecond)

Return obj.Next(0, 2)

End Function
Nov 21 '05 #2
* pe***@mclinn.co m (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.Bina ryWriter' 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******** ******@tk2msftn gp13.phx.gbl...
* pe***@mclinn.co m (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.Bina ryWriter' 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.c om> wrote in message
news:dc******** *************** **@posting.goog le.com...
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 "CreateFile A" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
<MarshalAs(Unma nagedType.Struc t)> _
ByRef lpSecurityAttri butes As SECURITY_ATTRIB UTES, _
ByVal dwCreationDispo sition As Integer, _
ByVal dwFlagsAndAttri butes As Integer, _
ByVal hTemplateFile As Integer) As Integer

<StructLayout(L ayoutKind.Seque ntial)> _
Private Structure SECURITY_ATTRIB UTES
Public nLength As Integer
Public lpSecurityDescr iptor 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 _
OpenFileWriteTh rough(ByVal FileName As String) As FileStream
Dim fh As Integer = CreateFile(File Name, _
GENERIC_WRITE, _
0, _
Nothing, _
OPEN_ALWAYS, _
FILE_FLAG_WRITE _THROUGH, _
0)
Return New FileStream(New IntPtr(fh), FileAccess.Writ e)

End Function
Public Shared Sub EraseFile(ByVal FileName As String)

For i As Integer = 0 To 8
Dim f As FileStream
f = OpenFileWriteTh rough(FileName)
Dim flen As Long = f.Length
f.Position = 0
Do While f.Position < flen
f.Write(ones, 0, Math.Min(ones.L ength, flen - f.Position))
Loop
f.Close()
f = OpenFileWriteTh rough(FileName)
f.Position = 0
Do While f.Position < flen
f.Write(zeros, 0, Math.Min(ones.L ength, flen - f.Position))
Loop
f.Close()
Next
File.Delete(Fil eName)
End Sub
Public Shared Sub Main(ByVal args() As String)
EraseFile("d:\v ictim.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
19391
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 OpenOffice. I know in *nix I can start by using "/" as the file name and doing a recursive listing of the file system from there. (Yes -- I know about permissions to read directories -- that's another issue I've already solved.) How do I find the...
3
3809
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 putting the file and the zipped file into C:\ temp. Any thoughts on what is causing this ? David B Hexham UK
3
1994
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 records the database is _huge_. Like 500 meg, although the csv files are only 25 meg in total. When I compact the database its still 100meg. How do I keep the size down? Second, I did a manual delete of 300,000 records yesterday by doing a...
13
9509
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 Dir(strLDB) <> "" Then Kill (strLDB) where strLDB is the path to the ldb file. The client advises that the ldb doesn't lurk after the program closes. Any ideas?
0
1058
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 files on this drive are flagged as read-only, but there is one piece of software that still creates a link to the file that prevents us from deleting it. We've tried using windows commands, dos shell commands with every possible combination of...
5
2678
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. But when I installed the app from a CD-ROM (Release folder is in D:), when I remove the app in Control Panel these problems occur: Control Panel does not delete the application folders. When I try to delete them I get message "Cannot delete file:...
8
14022
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 file... but nothing works. I keep getting the error: Cannot delete access_log: it is being used by another person or program. I tried shutting down services, I tried killing processes using the task manager, I tried rebooting, and still, I get...
3
2869
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, removing all the files/folders inside it. The problem i am having is that i can only delete files in the Windows/Temp folder, and i can't delete folders if they contain files, i know that you can't do this.
1
2548
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 to run this program from my computer (doesn't matter if it is on my local drive or on mapped one) the program runs ok. When somebody else trying to run this program from mapped drive, then they can not access this file. On local drives it works...
0
8335
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.