360的文件粉碎機還是很強大的,在我們客戶端winform升級的時候,必須將有些文件進行強力刪除後下載更新,如果刪除失敗很有可能整個 程序就無法更新到最新的版本,所以這裏參考了網上的資料整理了一個文件粉碎的小demo,總結了一個類出來,方便以後升級或者其他開發工作的調用
程序運行界面截圖
操作gif動畫圖片
整理的實際類,爲了以後使用,提高工作效率
//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2015 , ZTO , Ltd .
//-------------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace ZTO.WayBill.Utilities
{
/// <summary>
/// 文件幫助類
///
/// 修改紀錄
///
/// 2015-6-5 版本:1.0 YangHengLian 創建主鍵,注意命名空間的排序。
///
/// 版本:1.0
///
/// <author>
/// <name>YangHengLian</name>
/// <date>2015-6-5</date>
/// </author>
/// </summary>
public class FileHelper
{
/// <summary>
/// 強力粉碎文件,文件如果被打開,很難粉碎
/// </summary>
/// <param name="filename">文件全路徑</param>
/// <param name="deleteCount">刪除次數</param>
/// <param name="randomData">隨機數據填充文件,默認true</param>
/// <param name="blanks">空白填充文件,默認false</param>
/// <returns>true:粉碎成功,false:粉碎失敗</returns>
public static bool KillFile(string filename, int deleteCount, bool randomData = true, bool blanks = false)
{
const int bufferLength = 1024000;
bool ret = true;
try
{
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
FileInfo f = new FileInfo(filename);
long count = f.Length;
long offset = 0;
var rowDataBuffer = new byte[bufferLength];
while (count >= 0)
{
int iNumOfDataRead = stream.Read(rowDataBuffer, 0, bufferLength);
if (iNumOfDataRead == 0)
{
break;
}
if (randomData)
{
Random randombyte = new Random();
randombyte.NextBytes(rowDataBuffer);
}
else if (blanks)
{
for (int i = 0; i < iNumOfDataRead; i++)
rowDataBuffer[i] = 0;
}
else
{
for (int i = 0; i < iNumOfDataRead; i++)
rowDataBuffer[i] = Convert.ToByte(Convert.ToChar(deleteCount));
}
// 寫新內容到文件。
for (int i = 0; i < deleteCount; i++)
{
stream.Seek(offset, SeekOrigin.Begin);
stream.Write(rowDataBuffer, 0, iNumOfDataRead);
}
offset += iNumOfDataRead;
count -= iNumOfDataRead;
}
}
//每一個文件名字符代替隨機數從0到9。
string newName = "";
do
{
Random random = new Random();
string cleanName = Path.GetFileName(filename);
string dirName = Path.GetDirectoryName(filename);
int iMoreRandomLetters = random.Next(9);
// 爲了更安全,不要只使用原文件名的大小,添加一些隨機字母。
for (int i = 0; i < cleanName.Length + iMoreRandomLetters; i++)
{
newName += random.Next(9).ToString();
}
newName = dirName + "\\" + newName;
} while (File.Exists(newName));
// 重命名文件的新的隨機的名字。
File.Move(filename, newName);
File.Delete(newName);
}
catch
{
//可能其他原因刪除失敗了,使用我們自己的方法強制刪除
try
{
string fileName = filename;//要檢查被那個進程佔用的文件
Process tool = new Process { StartInfo = { FileName = "handle.exe", Arguments = fileName + " /accepteula", UseShellExecute = false, RedirectStandardOutput = true } };
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
foreach (Match match in Regex.Matches(outputTool, matchPattern))
{
//結束掉所有正在使用這個文件的程序
Process.GetProcessById(int.Parse(match.Value)).Kill();
}
File.Delete(fileName);
}
catch
{
ret = false;
}
}
return ret;
}
}
}