C # force close the current program process

Source: Internet
Author: User

C # force close the current program process

This article mainly introduces the implementation of C # To force close the current program process. This article directly provides the implementation code, which can be used to completely Kill the process without leaving any traces. For more information, see

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

/// <Summary>

/// Run the doscommand

/// DOS process shutdown command (ntsd-c q-p PID) PID is the process ID

/// </Summary>

/// <Param name = "command"> </param>

/// <Returns> </returns>

Public static string RunCmd (string command)

{

// An example is a Process type, which initiates a pipeline.

System. Diagnostics. Process p = new System. Diagnostics. Process ();

 

// The Process class has a StartInfo atomicity. This is ProcessStartInfo class, which includes some attention and methods. We will use its attention below:

 

P. StartInfo. FileName = "cmd.exe"; // set the program name

P. StartInfo. Arguments = "/c" + command; // you can specify the number of rows in the program.

P. StartInfo. UseShellExecute = false; // use of related Shell

P. StartInfo. RedirectStandardInput = true; // redirect the standard ingress

P. StartInfo. RedirectStandardOutput = true; // redirect the mark to export

P. StartInfo. RedirectStandardError = true; // redirect zookeeper output

P. StartInfo. CreateNoWindow = true; // you can specify a non-empty window.

 

P. Start (); // dynamic

 

// P. StandardInput. WriteLine (command); // you can also use this method to export the command to be executed.

// P. StandardInput. WriteLine ("exit"); // you must remember to add Exit. Otherwise, the next line of program will become available.

 

Return p. StandardOutput. ReadToEnd (); // get the result of the command response line from the extract stream

 

}

Add the following to Program. cs:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

. Static class Program

{

/// <Summary>

/// Main entry point of the application.

/// </Summary>

[STAThread]

Static void Main ()

{

Application. EnableVisualStyles ();

Application. SetCompatibleTextRenderingDefault (false );

Application. Run (new MainForm ());

// Force process Shutdown

String exeName = System. Diagnostics. Process. GetCurrentProcess (). MainModule. FileName;

String [] exeArray = exeName. Split ('\\');

 

FunctionClass. RunCmd ("taskkill/im" + exeArray [exeArray. Length-1] + "/f ");

}

}

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.