private void btn_CloseApp_Click(object sender, EventArgs e)
{
string filepath = RelaseAndGetHandleExePath();
CloseApp(filepath, tb_FileName.Text);
}
private void FixFileDate(FileInfo file, DateTimeOffset createdTime, DateTimeOffset modifiedTime)
{
// 更改文件的创建时间。
file.CreationTimeUtc = createdTime.UtcDateTime;
// 更改文件的更新时间。
file.LastWriteTimeUtc = modifiedTime.UtcDateTime;
// 更改文件最近一次访问的时间。
file.LastAccessTimeUtc = DateTimeOffset.Now.UtcDateTime;
}
/// <summary>
/// 删除占用文件的所有应用程序
/// </summary>
/// <param name="fileName"></param>
void CloseApp(string filepath, string fileName)
{
Process tool = new Process();
tool.StartInfo.FileName = filepath;
tool.StartInfo.Arguments = fileName + " /accepteula";
tool.StartInfo.UseShellExecute = false;
tool.StartInfo.RedirectStandardOutput = true;
tool.Start();
tool.WaitForExit();
string outputTool = tool.StandardOutput.ReadToEnd();
string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
bool IsClosed = false;
foreach (Match match in Regex.Matches(outputTool, matchPattern))
{
IsClosed = true;
Process.GetProcessById(int.Parse(match.Value)).Kill();
}
if (!IsClosed)
{
string filename = Path.GetFileNameWithoutExtension(fileName);
Process[] ProcessBuff = Process.GetProcessesByName(filename);
for (int i = 0; i < ProcessBuff.Length; i++)
{
string path = ProcessBuff[i].MainModule.FileName;
if (path == fileName)
ProcessBuff[i].Kill();
}
}
}
#region MyRegion
private string RelaseAndGetHandleExePath()
{
FileInfo handleInfo = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Windows) + "\\System32\\handle.exe");
if (!File.Exists(handleInfo.FullName))
{
if (!Directory.Exists(handleInfo.DirectoryName))
{
Directory.CreateDirectory(handleInfo.DirectoryName);
}
byte[] handleExeData = Properties.Resources.handle;
File.WriteAllBytes(handleInfo.FullName, handleExeData);
DateTime dt = new DateTime(2011, 10, 4, 8, 29, 0);
FixFileDate(handleInfo, DateTime.Now, dt);
var handleProc = Process.Start(handleInfo.FullName);//若第一次,则弹出提示框,需要点击agree同意才行
handleProc.WaitForExit();
}
return handleInfo.FullName;
}
#endregion
private void btn_WipeFile_Click(object sender, EventArgs e)
{
WipeFile(tb_FileName.Text, 1);
}
public void WipeFile(string filename, int timesToWrite)
{
try
{
if (File.Exists(filename))
{
//设置文件的属性为正常,这是为了防止文件是只读
File.SetAttributes(filename, FileAttributes.Normal);
//计算扇区数目
double sectors = Math.Ceiling(new FileInfo(filename).Length / 512.0);
// 创建一个同样大小的虚拟缓存
byte[] dummyBuffer = new byte[512];
// 创建一个加密随机数目生成器
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
// 打开这个文件的FileStream
FileStream inputStream = new FileStream(filename, 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(filename, dt);
File.SetLastAccessTime(filename, dt);
File.SetLastWriteTime(filename, dt);
// 删除文件
File.Delete(filename);
}
}
catch (Exception)
{
}
}
private void btn_SelectFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
tb_FileName.Text = openFileDialog1.FileName;
}
private void btn_OpenFile_Click(object sender, EventArgs e)
{
FileStream fs = File.Open(tb_FileName.Text, FileMode.Open);
}
private void button1_Click(object sender, EventArgs e)
{
KProtectProcess.KProcess.SelfProtection();
MessageBox.Show("Self Protection");
}
private void button2_Click(object sender, EventArgs e)
{
KProtectProcess.KProcess.UnLoadProtection();
MessageBox.Show("UnLoad self Protection");
}
private void button3_Click(object sender, EventArgs e)
{
uint pid = Convert.ToUInt32(tb_PID.Text);
KProtectProcess.KProcess.ProtectProcessID = pid;
KProtectProcess.KProcess.ProtectProcess();
MessageBox.Show("Protect Process:" + pid);
}
private void button4_Click(object sender, EventArgs e)
{
uint pid = Convert.ToUInt32(tb_PID.Text);
KProtectProcess.KProcess.ProtectProcessID = pid;
KProtectProcess.KProcess.UnLoadProtection();
MessageBox.Show("UnLoad Protection:" + pid);
}
请自行下载:handle.exe,然后添加到程序资源中
另外需要引用KProtectProcess.dll,手动将NKCore.dll复制到输出路径