C # Removal program itself "summary"

Source: Internet
Author: User

Source:http://www.cnblogs.com/Interkey/

Accidentally see a program that can be self-deleted, so understand how to implement. Then organize the following:

Ideas:

In. NET programs, since the running program is protected by the system and cannot delete itself, so the idea of self-deletion:

Start a new process before closing this program to open another program, call this program to delete the original program. The destruction of the external process is then completed.

Operation:

Method One: Open a new process in the program, delete the program, and then destroy the process itself

Process: First generate a BAT file, and then let bat perform the delete action, that is:
1. Generate the deleted bat
2. Run bat
3. Quick Exit
4. bat begins to delete exe
5. Bat Delete Bat

    <summary>    ///Remove program itself (this address: http://www.cnblogs.com/Interkey/p/DeleteItself.html) "recommended"///    </ 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 (                ":d el\r\n" +                "del \" {0}\ "\ r \ n" +                "if exist \" {0}\ "goto del\r\n" +                "del%0\r\n", Application. ExecutablePath));        }        Perform batch processing        winexec (vbatfile, 0);        End Exit        application.exit ();    }

[System.Runtime.InteropServices.DllImport ("kernel32.dll")]
public static extern uint WinExec (string lpcmdline, uint ucmdshow);

Method Two: Open a new process in the program, call the cmd command, remove the program.

    <summary>    ///Remove the program itself (this address: http://www.cnblogs.com/Interkey/p/DeleteItself.html)///    </summary >    private static void Deleteitselfbycmd ()    {        ProcessStartInfo psi = new ProcessStartInfo ("cmd.exe", "/C Ping 1.1.1.1-n 1-w > Nul & Del "+ Application.executablepath);        Psi. WindowStyle = Processwindowstyle.hidden;        Psi. CreateNoWindow = true;        Process.Start (PSI);        Application.exit ();    }

The following explains the meaning of the command:

CMD/C invoking the command window to execute DOS instructions
Ping 1.1.1.1-n 1-w > Nul Use the –w parameter of the ping command to specify a delay of 1 seconds to execute
& Connect Next command (for one row to execute multiple commands)
Del <Application.ExecutablePath> Delete executable files
Unfortunately this method because XP does not support the choice command, there is no test success. However, it was successfully removed from the Win8 x64 bit.

Summarize:

Method One: Use the bat file to remove the program + itself, but it has a temporary bat file generated.

Method Two: Using the delay effect of some DOS commands, it is very ingenious to delete the program automatically after the program exits. Also avoids the bat method, the problem of circular deletion. Of course, this approach should be essentially the same as the method.

However, because XP does not support the choice command, it is recommended to use the method one.

This address: http://www.cnblogs.com/Interkey/p/DeleteItself.html

Extended:

Suddenly think of the "accompanying process", that is, a program to generate two processes, timed to detect another process signal, if one of the processes is ended, another process immediately restarted the end of the process, the implementation of the program can not end unexpectedly (especially the Windows End Task) effect!

Approach to the end of this accompanying process (a major worker process, a companion process): Quickly end two processes, and of course, if two processes are short enough to detect, this method is not necessarily effective. However, the goal of ending two processes can be achieved with another false accompanying process.

C # Removal program itself "summary"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.