How can I make my .NET application erase itself?

前端 未结 11 713
梦如初夏
梦如初夏 2020-12-08 10:37

How can I make my C# app erase itself (self-destruct)? Here\'s two ways that I think might work:

  • Supply another program that deletes the main program. How is t
11条回答
  •  星月不相逢
    2020-12-08 11:25

    Works in Windows 7 & 8, **ENSURE you run your application with admin privileges or you will get an error.

    This code exists elsewhere so I can't take full credit I found I made it work for me by adding "Application.Exit();"

    static void autodelete()
    {
        string batchCommands = string.Empty;
        string exeFileName = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty).Replace("/", "\\");
    
        batchCommands += "@ECHO OFF\n";                         // Do not show any output
        batchCommands += "ping 127.0.0.1 > nul\n";              // Wait approximately 4 seconds (so that the process is already terminated)
        batchCommands += "echo j | del /F ";                    // Delete the executeable
        batchCommands += exeFileName + "\n";
        batchCommands += "echo j | del deleteMyProgram.bat";    // Delete this bat file
    
        File.WriteAllText("deleteMyProgram.bat", batchCommands);
    
        Process.Start("deleteMyProgram.bat");
        Application.Exit();
    }
    

提交回复
热议问题