Secure Delete
Method Name: SecureDelete
Summary:
This method securely deletes a file by overwriting it with random data multiple times, then renaming and finally deleting the file.
Syntax:
public static void SecureDelete(string filePath, int passes)
Parameters:
filePath
(string): The path of the file to be securely deleted.passes
(int): The number of times the file should be overwritten with random data.
Description:
The SecureDelete
method performs the following steps:
- Checks if the file exists.
- If it does, it sets the file’s attributes to normal in case it’s read-only.
- Calculates the total number of sectors in the file.
- Creates a dummy buffer the same size as the sector.
- Creates a cryptographic Random Number Generator, which is used to fill the file with random data.
- Opens a FileStream to the file.
- Writes random data to each sector of the file for a specified number of passes.
- After overwriting, it closes the FileStream.
- As an extra measure, it calculates a random filename to rename the file to before deleting it.
If an exception occurs during reading or writing, it prints out an error message and closes all open streams.
Returns
This method does not return a value.
Exceptions:
This method catches and handles general Exception
. If an exception is caught, an error message is printed to the console.
Remarks:
This method should securely erase most files, making them unrecoverable with standard tools. However, please note that this might not work on all types of storage media due to hardware-level features such as wear-levelling on SSDs. Also, remember that this method can be time-consuming for large files or when a large number of passes is specified, as it needs to overwrite every single sector in the file multiple times.
The "Wipe File" (Secure Deletion) form has got a minimum & maximum possible value for the number of passes it should make, now. This means that the form cannot make any number of passes fewer than 3, or greater than 100.