Unfortunately, this is not entirely possible in C # and not in C / C ++, even if you are writing a kernel-mode driver.
Quote from the Bleachbit documentation :
Shredding an individual file properly suggests that its location can be fully known, but basically it can only be known in one ideal case. An ideal case has three characteristics:
- File size has never been reduced due to editing. Imagine starting with a 3 MB spreadsheet, editing it up to 1 MB (using the spreadsheet application) and asking the cleaning application to remove the 1 MB version: the cleaner does not have the ability to find out where 2 MB was missing on the physical hard disk. (Remember: file systems often do not store files continuously, so you cannot assume that the missing part was immediately after the known part.)
- The file never moved. Imagine spreadsheet software saving a document by writing a new copy to a temporary file, deleting the old copy and renaming the temporary file to the original name. In this case, the cleaner application does not know where any of the old tables are located.
- The file system overwrites files to the same place. This is a good guess. On Windows NTFS and Linux, the most common ext3 configuration (which is the default for Ubuntu 9.10 and other Linux distributions) overwrites files in one place, but transparent disk compression, encryption, and sparse files cannot overwrite files in place.
Next: When a region of a modern hard drive is damaged, it automatically reassigns a bad sector to a spare one. These operations are performed at the discretion of the driveβs firmware, and neither the operating system nor applications are aware of this, therefore cleaning the disk ignores the damaged area.
Having said that, you can (although not easily) find out which sectors of the disk the file currently occupies. This requires, however, that your application (at least partially) understand the file system used and how this file system stores files on the underlying medium.
Finally, the question remains: what additional security would you gain by indicating all sectors that your file occupies and filling them with 0s, instead of doing
using(var fs = new System.IO.FileStream(@"m:\delme.zip", FileMode.Open, FileAccess.Write, FileShare.None)) { var zeros = new byte[fs.Length]; fs.Write(zeros, 0, zeros.Length); }
source share