C# 程序自我删除

方法:程序中打开新的进程,删掉程序后,再销毁进程本身
步骤:
1、生成删除的BAT
2、运行BAT
3、快速退出
4、BAT开始删除EXE
5、BAT删除BAT
实现:

/// <summary>
/// 删除程序自身(本文地址:http://www.cnblogs.com/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#删除程序自身【总结】

posted @ 2025-08-16 10:07  BigBosscyb  阅读(32)  评论(0)    收藏  举报