Click here to Skip to main content
15,886,833 members
Articles / Desktop Programming / Win32
Tip/Trick

Free Space Cleaner

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Aug 2012GPL32 min read 23.8K   1.4K   7   7
Free space cleaner to overwrite empty space on logical disks

Introduction

Free Space Cleaner is a C# Windows Forms Application that attempts to wipe the free space of a hard disk via a simple one pass write of all zeros or random bytes if desired. This tip is intended to present users with a simple insight to logical disks, hard drives, and free space wiping.

Background

I found this program on another web site while I was looking for a free space wiper for my SSD. I realized that it was a very simple program and could use some improvement. I saw no similar programs/articles on CodeProject so I decided to decompile the program, improve it, and write a tip about it. The original author of the base code was not specified; so if anyone is familiar with the author/recognizes the GUI and can tell me who it is, I would like to give credits for the original design.

Using the Code

The code uses a BackgroundWorker object in order to not clog up the GUI thread and too many system resources while cleaning the free space.

In order to query the system for your logical disk drives, a ManagementObjectSearcher is used with a query. Once you've found the logical devices on your system, you have to query for the hard drives associated to the logical disk.

C#
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");  

After you have chosen the disk you want to clear, this program uses a DllImport to write files to the hard disk and then delete it once the disk is full.

C#
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess,
                                                      FileShare dwShareMode,
                                                      IntPtr lpSecurityAttributes,
                                                      FileMode dwCreationDisposition,
                                                      int dwFlagsAndAttributes,
                                                      IntPtr hTemplateFile);

This Kernel32 import returns a file handle to open a file stream object with, and allows us to write the zeros or random data to. See the attached project to see the specifics of writing the file.

Points of Interest

It is willing to note that this code was decompiled from the original program; this can be shown in the strange layout of the InitializeComponent method. I liked this program for cleaning out my SSD when the performance started choking because there were chunks of "rewrite" flagged files on the drive. I realized it was written in C# and decided I could improve it. I've also uploaded the code to Google Code with git at the following location: http://code.google.com/p/freespace-cleaner/.

History

  • 0.7 Initial release version of article and application code

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
United States United States
I am an undergraduate in Florida currently pursuing a B.S. in Computer Engineering. I enjoy programming in Java and C#.

Comments and Discussions

 
QuestionBug fix Pin
WolfmanProper13-Sep-14 6:56
WolfmanProper13-Sep-14 6:56 
QuestionGood tip! Pin
Volynsky Alex26-Aug-12 0:44
professionalVolynsky Alex26-Aug-12 0:44 
GeneralThoughts Pin
PIEBALDconsult25-Aug-12 9:48
mvePIEBALDconsult25-Aug-12 9:48 
GeneralRe: Thoughts Pin
Rich Turner25-Aug-12 17:43
Rich Turner25-Aug-12 17:43 
GeneralRe: Thoughts Pin
PIEBALDconsult26-Aug-12 4:01
mvePIEBALDconsult26-Aug-12 4:01 
GeneralRe: Thoughts Pin
Rich Turner29-Aug-12 19:09
Rich Turner29-Aug-12 19:09 
GeneralRe: Thoughts Pin
PIEBALDconsult30-Aug-12 3:48
mvePIEBALDconsult30-Aug-12 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.