C # Delete the program itself [summary ],
Source:Http://www.cnblogs.com/Interkey/
I accidentally saw a program that can be deleted, so I learned how to implement it. Then, sort the information as follows:
Ideas:
In. NET programs, because the running programs are protected by the system, they cannot be deleted by themselves, so the idea of self-deletion is as follows:
Start a new process before closing this program to open another program and call this program to delete the original program. Then, the external process is destroyed.
Operation:
Method 1: 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 Execute the delete action, that is:
1. Generate the deleted BAT
2. Run BAT
3. quick exit
4. BAT starts to delete the EXE.
5. BAT deletes BAT.
/// <Summary> /// Delete the program itself (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 (": del \ r \ n "+" del \ "{0} \" \ r \ n "+" if exist \ "{0} \" goto del \ r \ n "+ "del % 0 \ r \ n ", application. executablePath);} // ************* execute the batch processing WinExec (vBatFile, 0 ); // ************* end and exit the Application. exit ();}
[System. Runtime. InteropServices. DllImport ("kernel32.dll")]
Public static extern uint WinExec (string lpCmdLine, uint uCmdShow );
Method 2: open a new process in the program, call the cmd command, and delete the program.
/// <Summary> /// Delete the program itself (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 1000> 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 call the command window to execute dos Commands
Ping 1.1.1.1-n 1-w 1000> Nul uses the-w parameter of the ping command to specify a delay of 1 second for execution.
& Connect to the next command (used to execute multiple commands in one line)
Del <Application. ExecutablePath> delete an executable file
Unfortunately, this method was not successfully tested because xp does not support the Choice command. However, it is successfully deleted on the Win8 x64 bit.
Summary:
Method 1: Use the bat file to delete the program + itself, but it has temporary bat file generation.
Method 2: using the latency of some DOS commands, the idea of automatically deleting a program after the program exits is very clever. It also avoids the problem of loop deletion in bat mode. Of course, this method should be similar to method 1 in essence.
However, because xp does not support the Choice command, we recommend method 1 for usage.
Address: http://www.cnblogs.com/Interkey/p/DeleteItself.html
Extension:
Suddenly think of "accompanied process", that is, a program generates two processes and regularly detects the signal of another process. If one process is terminated, the other process immediately restarts the terminated process, the program cannot end abnormally (especially in Windows!
For this accompanied process (a major working process, a accompanied process) to end: to quickly end two processes, of course, if the detection of the two processes is short enough, this method is not necessarily effective. However, you can end two processes with another fake accompanying process.