How can I make my .NET application erase itself?

前端 未结 11 571
梦如初夏
梦如初夏 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:12

    I know reflector deletes itself if you use an old version and choose not to update. You might try to figure out what it does. I would start with FileMon and see if it spawns any processes to achieve this.

    0 讨论(0)
  • 2020-12-08 11:15

    Since my application (a Windows Service) is installed via the Windows Installer, I self-delete using this:

    Dim uninstall_params As String = "/x {MY-PRODUCTS-GUID} /qn /norestart REBOOT=ReallySuppress"
    proc.StartInfo = New ProcessStartInfo("msiexec.exe", uninstall_params)
    proc.Start()
    Environment.Exit(-1)
    

    Sorry--it's in VB, but it should be easily convertible to C#.

    0 讨论(0)
  • 2020-12-08 11:17

    sorted by NJ c# the other codes does not work so its simple if u create bath file that loops to del application and the batch file itself u can use takkill command to kill the process if u dont want to use application.close method

            `string delname = "test.cmd";
            string fileurl = Application.ExecutablePath;
            System.IO.StreamWriter file = new System.IO.StreamWriter(delname);
            file.WriteLine(":Repeat");
            file.WriteLine("del \"" + fileurl + "\"");
            file.WriteLine("if exist \"" + fileurl + "\" goto Repeat");
            file.WriteLine("del \"" + delname + "\"");
            file.Close();
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = delname;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            Process.Start(startInfo);`
    

    ` Th3 3e3 one is not 3d parts ov one I5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    0 讨论(0)
  • 2020-12-08 11:21

    A correction to @Bobby answer, in case people will find it useful - executable path needs to be quoted. Additionally, below is setting cmd.exe window to be hidden (otherwise it flashes as a black console window) and converted to run without relying on System.Windows.Forms assembly (the Application class).

     
    var exepath = Assembly.GetEntryAssembly().Location;              
    var info = new ProcessStartInfo("cmd.exe", "/C ping 1.1.1.1 -n 1 -w 3000 > Nul & Del \"" + exepath + "\"");
    info.WindowStyle = ProcessWindowStyle.Hidden;
    Process.Start(info).Dispose();
    Environment.Exit(0);
    
    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
提交回复
热议问题