Hey guys!

Two questions: how can I completely wipe a file from disk with vb.net? I found a code in C#, converted it to VB.net online but didn't understand a letter. I was thinking on a wiper like CCleaner has, but for a selected files only.

And, how can I manage drag-n-drop functions? Like... drag something into my form (or my app's exe) and do something with it?


Recommended Answers

All 6 Replies

For drag and drop, check out this thread.

About "wiping" a file from the disk, do you want to just delete the file or shred the file a few times, then delete?

I think that will make no difference. I just want the data to be unrecoverable, not easily though.

If you just want to Delete a File from the system, use:

IO.File.Delete("full path of file to delete here")

Otherwise, let me know if this helps.
1 Button, 1 ProgressBar(useful when shredding the file over 10,000 times :D)

Public Class Form1
    Private iNumberOfTimesToShred As Integer = 10 '// Times to load/manipulate/save the File.
    Private iNumberOfCharactersToInjectPerShred As Integer = 25 '// Characters in sCharactersToInjectIntoFile. 
    '// Loaded a image in Notepad++ and found tons of cool char. to inject into the File Randomly.
    Private sCharactersToInjectIntoFile As String = "›ÙÅ×¾e…€è½Ø`WØÙÛlÕÎÓ¶mßSöÌ‹ïº;ϼç;¶mßSöÌ‹ïº;ϼç;éHO>òßp¦B‚ƺŒßf=¦ì°¡óÓlÜòL›™tØ–l;a«wŸù1òag½1â;éHO>Ó|¤Õì0ÙšöÓqÖ#W[W­’î;uKŽ1š•Ý3Z±5²èqÃnͦíÓ0íè%;xê%;õ¯õëï}lKVm´µ[Ò,mÿIË:ù¬í>¬UÕ©:½ã¾¬Ž—fw}ýnÓe,&¹xDEb{XÑê=­tmš)ý®ƒúå=¤!î(Ê–}òeè×}Ý‘7¬v÷ÉöPöíïÿ«ýâ÷±¿üãú÷7ŸµÖî/RËÊk¨SI‹"
    Private sMain As String = Nothing '// String to load File in and manipulate the file content.
    Private sTemp, sTemp2 As String '// Temp Strings used as needed.
    Private rnd As New Random '// Used to Randomize which characters to replace and which characters to inject.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        If ofd.ShowDialog = DialogResult.OK Then
            sMain = IO.File.ReadAllText(ofd.FileName, System.Text.Encoding.Default) '// Load File as String.
            ProgressBar1.Maximum = iNumberOfTimesToShred : ProgressBar1.Value = 0 '// Set/Reset Properties of Progressbar.
            Do Until ProgressBar1.Value = iNumberOfTimesToShred '// Loop until the ProgressBar.Value is the Total of times to Shred.
                For i As Integer = 1 To iNumberOfCharactersToInjectPerShred '// inject random char. in file.
                    sTemp = sCharactersToInjectIntoFile.Substring(rnd.Next(0, sCharactersToInjectIntoFile.Length), 1) '// get random char.
                    rnd = New Random : sMain = sMain.Insert(rnd.Next(0, sMain.Length), sTemp) '// insert at random into File.
                sTemp = sMain.Substring(rnd.Next(0, sMain.Length), 1) '// select a random char.
                rnd = New Random : sTemp2 = sMain.Substring(rnd.Next(0, sMain.Length), 1) '// select a random char.
                sMain = sMain.Replace(sTemp, sTemp2) '// replace selected characters.
                If sMain.Contains(vbCrLf) Then sMain = sMain.Replace(vbCrLf, "") '// Remove all Line Breaks.
                If sMain.Contains(" ") Then sMain = sMain.Replace(" ", "") '// Remove all Empty Spaces.
                'MsgBox(sMain) '// Uncomment if you want to Preview the File Content before saving.
                Try '// some Files might throw an error when saving, not always on the next pass(es).
                    IO.File.WriteAllText(ofd.FileName, sMain)
                Catch ex As Exception
                End Try
                Application.DoEvents() '// Let the Application Finish up before moving on to the next Loop.
                ProgressBar1.Value += 1
            '//-------- TESTING PURPOSES ONLY
            Try '// Might crash the app if shredding a Application(.exe).
                Process.Start(ofd.FileName) '// Try and preview the File. :D
            Catch ex As Exception
            End Try '--------\\
            '// Delete the File from the System.
        End If
    End Sub
End Class

I'm personally unclear on exactly how shredding a file works and just from the online resources, I managed to figure out that it overwrites a file over and over a few times, most of the time with the same file content just added to the file.
..To me, that seems quite easy to recover, if having the knowledge to recover a Permanently Deleted File.

With the above code provided, you can load all types of files, including .exe, and inject/replace characters in the file at random.
It saves the file on every pass with a different random shred, thus leaving the file quite un....something.
...Well, let's just say that the file will "never be the same". (echo, echo, echo):D

To thoroughly test the provided code, make a few duplicates of a similar file, preferably a .txt file and run each copy of the file through the shredder. The results vary from file to file. If needed, increase the numbers of times to shred and/or increase the characters to inject into the file on each shred pass.
..I personally have no use for such code or to put my beloved p.c. through such a process as to shredding files, but to those that do have a use, I hope this helps.

commented: Great insight! +1

Yeah, nice code, but now I really don't find any utility to it, since there are many shredders and we don't need one more =)

THanks for the codes, anyway!

Just thought I'd help.

And helped =) But, seems to not worth the efford to make a shredder since there are lots of programs that do this.

Anyway, I will mostly use it for learning.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.