I have the code below and the file seems to be overwritten as they should.
I have tested Ontrack EasyRecovery and PC Inspector File Recovery and I wasn't able to get any viewable data back.
The only annoying thing is that booth recovery-applications were able to bring back the filename of the overwritten file which I don't want of course.
If I don't rename the file before overwriting it, all that is recovered is a zero-byte file with the correct filename.
If I rename it first, I can recover a file with the correct filename and size but the data is destroyed.
So what I'm asking for is, is there a way to completely delete a file or change the name of the file permanently so it can't be recovered.
Expand|Select|Wrap|Line Numbers
- private void WipeFile(string filename, int timesToWrite)
- {
- if(File.Exists(filename))
- {
- // Rename the file
- string newFileName = Path.GetDirectoryName(filename) + GetRandomFileName();
- File.Move(filename, newFileName);
- filename = newFileName;
- // Create a buffer for the dummy-data
- byte[] dummyBuffer = new byte[new FileInfo(filename).Length];
- RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
- // Fill the buffer with random data
- rng.GetBytes(dummyBuffer);
- // Write to the file n times
- for (int timesWritten = 0; timesWritten < timesToWrite; timesWritten++)
- {
- FileStream inputStream = new FileStream(filename, FileMode.Create);
- inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
- // Get some new dummy-data
- rng.GetBytes(dummyBuffer);
- inputStream.Close();
- }
- // Create a zero-byte file
- FileStream tmpStream = new FileStream(filename, FileMode.Create);
- tmpStream.Close();
- File.Delete(filename);
- }
- }