[C#] 強力刪除檔案
內建檔案刪除方法 File.Delete 是將該檔案空間標記可寫入若沒有後續資料寫入,該儲存位置的資料是可以讀回復原
這邊使用直接將儲存檔案的磁碟空間來回寫入隨機假資料
經過這樣的操作就很難復原檔案,實際上真地『刪除』了public static void WipeFile(string FilePath, int TimesToWrite = 20)
{
try
{
if (File.Exists(FilePath))
{
File.SetAttributes(FilePath, FileAttributes.Normal);
double sectors = Math.Ceiling(new FileInfo(FilePath).Length / 512.0);
byte[] dummyBuffer = new byte;
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
FileStream inputStream = new FileStream(FilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
for (int currentPass = 0; currentPass < TimesToWrite; currentPass++)
{
inputStream.Position = 0;
for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)
{
rng.GetBytes(dummyBuffer);
inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);
}
}
inputStream.SetLength(0);
inputStream.Close();
DateTime dt = new DateTime(2037, 1, 1, 0, 0, 0);
File.SetCreationTime(FilePath, dt);
File.SetLastAccessTime(FilePath, dt);
File.SetLastWriteTime(FilePath, dt);
File.Delete(FilePath);
}
}
catch (Exception) { }
} 實用,感謝大大的分享 !:D
頁:
[1]