C# 程序自我刪除
方法:程序中打開新的進程,刪掉程序后,再銷毀進程本身
步驟:
1、生成刪除的BAT
2、運行BAT
3、快速退出
4、BAT開始刪除EXE
5、BAT刪除BAT
實現:
/// <summary>
/// 刪除程序自身(本文地址:http://www.rzrgm.cn/Interkey/p/DeleteItself.html)【推薦】
/// </summary>
private static void DeleteItself()
{
string vBatFile = Path.GetDirectoryName(Application.ExecutablePath) + "\\DeleteItself.bat";
using (StreamWriter vStreamWriter = new StreamWriter(vBatFile, false, Encoding.Default))
{
vStreamWriter.Write(string.Format(
":del\r\n" +
" del \"{0}\"\r\n" +
"if exist \"{0}\" goto del\r\n" +
"del %0\r\n", Application.ExecutablePath));
}
//************ 執行批處理
WinExec(vBatFile, 0);
//************ 結束退出
Application.Exit();
}
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine, uint uCmdShow);
參考:
C#刪除程序自身【總結】

浙公網安備 33010602011771號