Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Free Space Cleaner

0.00/5 (No votes)
25 Aug 2012 1  
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.

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.

  [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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here