Click here to Skip to main content
Click here to Skip to main content

Securely Delete a File using .NET

, 14 Jan 2008
Rate this:
Please Sign up or sign in to vote.
How to securely delete a file using .NET

Introduction

This is an attempt to make it a little more secure to delete files using C#. Let's face it: using File.Delete doesn't really delete the file; it just makes the OS think that the file doesn't exist. The space on the disc that the file occupied is marked as free and available for writing, but the file's data is not removed and can be recovered quite easily. The file is not gone until the space it occupied is overwritten, preferably several times.

I have put together a simple class that can be used to accomplish the procedure of writing garbage data to the file before deleting it. To take it a step further, I truncate the file to 0 bytes and change the file-dates. However, the step to change the file dates only seems to work on FAT 16/32 and not NTFS. After running several tests with Ontrack EasyRecovery, PC Inspector File Recovery and GetDataBack, I could not recover any of the files. I even ran Disk Investigator after deleting a text-file, checking the individual clusters, and I couldn't get a single word back from the file.

Background

The background to this project is simple. After using file-wipers/shredders for years now, I wanted to see what could be accomplished with .NET.

Using the Code

There is only one method that you need to call, namely WipeFile and the code is as shown below. So, all you really have to do is call WipeFile and supply the full path of the file to be deleted, as well as the number of times you want to overwrite it.

public void WipeFile(string filename, int timesToWrite)
{
    try
    {
        if (File.Exists(filename))
        {
            // Set the files attributes to normal in case it's read-only.

            File.SetAttributes(filename, FileAttributes.Normal);

            // Calculate the total number of sectors in the file.
            double sectors = Math.Ceiling(new FileInfo(filename).Length/512.0);

            // Create a dummy-buffer the size of a sector.

            byte[] dummyBuffer = new byte[512];

            // Create a cryptographic Random Number Generator.
            // This is what I use to create the garbage data.

            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            // Open a FileStream to the file.
            FileStream inputStream = new FileStream(filename, FileMode.Open);
            for (int currentPass = 0; currentPass < timesToWrite; currentPass++)
            {
                UpdatePassInfo(currentPass + 1, timesToWrite);

                // Go to the beginning of the stream

                inputStream.Position = 0;

                // Loop all sectors
                for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
                {
                    UpdateSectorInfo(sectorsWritten + 1, (int) sectors);

                    // Fill the dummy-buffer with random data

                    rng.GetBytes(dummyBuffer);

                    // Write it to the stream
                    inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
                }
            }

            // Truncate the file to 0 bytes.
            // This will hide the original file-length if you try to recover the file.

            inputStream.SetLength(0);

            // Close the stream.
            inputStream.Close();

            // As an extra precaution I change the dates of the file so the
            // original dates are hidden if you try to recover the file.

            DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
            File.SetCreationTime(filename, dt);
            File.SetLastAccessTime(filename, dt);
            File.SetLastWriteTime(filename, dt);

            // Finally, delete the file

            File.Delete(filename);

            WipeDone();
        }
    }
    catch(Exception e)
    {
        WipeError(e);
    }
}

I have added some events just to be able to keep track of what's happening during the process.

  • PassInfoEvent - Returns which pass is running and the total number of passes to be run.
  • SectorInfoEvent - Returns which sector is being written to and the total number of sectors to be written to.
  • WipeDoneEvent - An indicator that the wipe process is done.
  • WipeErrorEvent - Returns the exception if anything went wrong.

Acknowledgements

I'd like to thank PIEBALDconsult for his time and help in the forum while working with this.

History

  • 7 January, 2008 -- Original version posted
  • 14 January, 2008 -- First update

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Johan Martensson
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

 
QuestionVB.NET Equivalent? [modified] PinmemberJames Statom18-Dec-13 9:08 
QuestionWhy do you assume 512-byte sectors? Pinmemberyjj28-Jan-08 5:14 
AnswerRe: Why do you assume 512-byte sectors? PinmemberJohan Martensson28-Jan-08 5:58 
GeneralRe: Why do you assume 512-byte sectors? Pinmemberyjj28-Jan-08 20:41 
GeneralNotice about System Resotre PinmemberPrzemyslaw Celej14-Jan-08 12:55 
GeneralSome doubts [modified] PinmvpLuc Pattyn7-Jan-08 14:55 
GeneralRe: Some doubts PinmemberPIEBALDconsult7-Jan-08 16:03 
GeneralRe: Some doubts PinmvpLuc Pattyn7-Jan-08 16:28 
GeneralRe: Some doubts PinmemberJohan Martensson7-Jan-08 21:04 
GeneralRe: Some doubts PinmvpLuc Pattyn8-Jan-08 2:18 
GeneralRe: Some doubts PinmemberJohan Martensson8-Jan-08 4:22 
GeneralWhoops PinmemberPIEBALDconsult7-Jan-08 5:13 
GeneralRe: Whoops PinmemberJohan Martensson7-Jan-08 6:57 
GeneralVery Nice Pinmembermerlin9817-Jan-08 4:36 
GeneralRe: Very Nice PinmemberJohan Martensson7-Jan-08 5:17 
QuestionFilling the file with 0 would work? PinmemberJcmorin7-Jan-08 3:44 
AnswerRe: Filling the file with 0 would work? PinmemberJohan Martensson7-Jan-08 4:03 
QuestionRe: Filling the file with 0 would work? PinmemberVladimir N.8-Jan-08 19:21 
GeneralRe: Filling the file with 0 would work? PinmemberJohan Martensson8-Jan-08 21:38 
AnswerRe: Filling the file with 0 would work? PinmemberVladimir N.8-Jan-08 22:33 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Jan 2008
Article Copyright 2008 by Johan Martensson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid