C # Forces the current program process to close (completely kill without leaving traces)

Source: Internet
Author: User

Original: C # forces the current program process to close (completely kill without leaving traces)

C # Forces the current program process to close (completely kill without leaving traces)

C # code
  1. // <summary>   
  2. /// Run DOS command
  3. ///DOS Shutdown process command (ntsd-c q-p PID) PID for process ID
  4. // </summary>
  5. /// <param name= "command" ></param>
  6. // <returns></returns>
  7. public static string Runcmd (String c15> command)
  8. {
  9. //Example a process to activate a stand-alone process
  10. System.Diagnostics.Process p = New   System.Diagnostics.Process ();
  11. //process has a startinfo, which is processstartinfo, including some of its own nature and methods, and below we have used his several:
  12. p.StartInfo.FileName = "cmd.exe"           ; //Set Program name
  13. p.startinfo.arguments = "/C"    + command; //Set program to perform the parameters
  14. P.startinfo.useshellexecute = false        ; //Close the use of the shell
  15. p.startinfo.redirectstandardinput = true   ; //redirect standard input
  16. p.startinfo.redirectstandardoutput = true  ; //redirect standard output
  17. P.startinfo.redirectstandarderror = true   ; //redirect wrong output
  18. P.startinfo.createnowindow = true          ; //Setup does not display the window
  19. P.start (); //activation
  20. //p.standardinput.writeline (command); You can also enter commands to execute in this way
  21. //p.standardinput.writeline ("exit"); But remember to add the exit or the next line of code will be
  22. return p.standardoutput.readtoend (); //from the output stream to get the command to perform the results
  23. }
<summary>///Run DOS command//DOS shutdown process command (ntsd-c q-p PID) PID for process ID//</summary> <param name= "command" ></param>///<returns></returns> public Stati C string Runcmd (String command) {//An instance of a process, activating a stand-alone process System.Diagnostics.Process p                = new System.Diagnostics.Process ();           The process has a startinfo, which is processstartinfo, including some of its own nature and methods, and the following we have used his several: p.StartInfo.FileName = "cmd.exe";    Set Program Name p.startinfo.arguments = "/C" + command;        Set Program to execute parameters P.startinfo.useshellexecute = false;   Close the shell using P.startinfo.redirectstandardinput = true;  REDIRECT Standard input p.startinfo.redirectstandardoutput = true;   REDIRECT Standard output P.startinfo.redirectstandarderror = true;          redirect wrong output P.startinfo.createnowindow = true;   Setup does not display the window P.start ();      Activate          P.standardinput.writeline (command);        You can also enter the command//p.standardinput.writeline ("Exit") to execute in this way;        But remember to add the exit or the next line of the program will return to P.standardoutput.readtoend (); Get command from output stream to perform results}

In Program.cs Add the following

C # code
  1. Static class program
  2. {
  3. // <summary>
  4. /// The main entry point of the application.   
  5. // </summary>
  6. [STAThread]
  7. static void Main ()
  8. {
  9. Application.enablevisualstyles ();
  10. Application.setcompatibletextrenderingdefault (false);
  11. Application.Run (new mainform ());
  12. //Force shutdown process
  13. string exename = System.Diagnostics.Process.GetCurrentProcess ().   Mainmodule.filename;
  14. string[] Exearray = Exename.split (' \ \ ');
  15. functionclass.runcmd ("Taskkill/im" + exearray[exearray.length-1] + "/   F ");
  16. }
  17. }

C # Forces the current program process to close (completely kill without leaving traces)

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.