-
-
Save AramRafeq/f084a211d78d08e48cc1985f26550e71 to your computer and use it in GitHub Desktop.
how permadelet works 2 https://devstree.io/how-permadelete-works-2/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="*" /> | |
<RowDefinition Height="*" /> | |
</Grid.RowDefinitions> | |
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center"> | |
<TextBlock Text="File Name: " FontSize="24"/> | |
<TextBox x:Name="txtFileName" Width="280" FontSize="18"/> | |
</StackPanel> | |
<Button x:Name="buttonShred" Content="Shred" FontSize="24" Padding="30, 15" HorizontalAlignment="Center" | |
VerticalAlignment="Center" Grid.Row="1" Click="buttonShred_Click" /> | |
</Grid> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
namespace FileShredder | |
{ | |
public class Shredder | |
{ | |
private const int MAX_BUFFER_SIZE = 1024 * 1024; // One Megabyte | |
static private Random _random = new Random(); // Used to get random numbers to replace over the original file data | |
// The rest of the class goes here... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void OverWriteFile(FileInfo file) | |
{ | |
// 1. Open a stream from the file | |
using (var stream = new FileStream(file.FullName, FileMode.Open, FileAccess.Write, FileShare.None)) | |
{ | |
// 2. Generate random numbers and store them in an array | |
var buffer = new byte[MAX_BUFFER_SIZE]; | |
for (int i = 0; i < MAX_BUFFER_SIZE; i++) | |
{ | |
// Random numbers are from 0 to 255. because each of them replace a byte of the file. | |
buffer[i] = (byte)_random.Next(0, 255); | |
} | |
// 3. Now replace the file's actual content with the array of random numbers | |
for (var length = file.Length; length > 0; length -= MAX_BUFFER_SIZE) | |
{ | |
// Make sure we don't write more bytes than the file size | |
int bytesToWrite = (length > MAX_BUFFER_SIZE) ? MAX_BUFFER_SIZE : (int)length; | |
// Write the whole buffer over the file | |
stream.Write(buffer, 0, bytesToWrite); | |
// Flush as soon as possible. To make sure that the file is overwritten | |
stream.Flush(); | |
} | |
// 4. Set file size to 0 | |
stream.SetLength(0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void DestroyFileMetadata(FileInfo file) | |
{ | |
// 1. Get the folder name that has the file | |
var directoryName = Path.GetDirectoryName(file.FullName); | |
// 2. Rename the file 5 times to remove it from the file system table. | |
for (var round = 0; round < 5; round++) | |
{ | |
var newPath = Path.Combine(directoryName, Path.GetRandomFileName()); | |
file.MoveTo(newPath); | |
} | |
// 3. Set all of the dates of the to Jan 1st 2000. | |
var newTime = new DateTime(2000, 1, 1, 0, 0, 1); | |
file.LastWriteTime = newTime; | |
file.CreationTime = newTime; | |
file.LastAccessTime = newTime; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ShredFile(string path) | |
{ | |
// 1. Make sure the file exists | |
if (!File.Exists(path)) | |
throw new ArgumentException("The file does not exist: " + path); | |
// 2. Get the file info | |
FileInfo file = new FileInfo(path); | |
// 3. Shred the file | |
OverWriteFile(file); | |
DestroyFileMetadata(file); | |
File.Delete(path); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void buttonShred_Click(object sender, RoutedEventArgs e) | |
{ | |
try | |
{ | |
Shredder.ShredFile(txtFileName.Text); | |
MessageBox.Show("The file was shredded successfuly!", "Success!"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message, "There was an error", MessageBoxButton.OK, MessageBoxImage.Error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment