I haven't touched C # development for more than a year. I 'd like to review winform knowledge:
C # enable and disable processes to obtain the process list;
There is a picture with the truth:
1 Using System;
2 Using System. Collections. Generic;
3 Using System. componentmodel;
4 Using System. Data;
5 Using System. drawing;
6 Using System. text;
7 Using System. Windows. forms;
8 Using System. diagnostics;
9 Using System. Threading;
10
11 Namespace Processexample
12 {
13
14 Public Partial Class Form1: Form
15 {
16
17 Public Form1 ()
18 {
19 Initializecomponent ();
20 }
21
22 Private Void Buttonstart_click ( Object Sender, eventargs E)
23 {
24 Process1.startinfo. filename = " Notepad.exe " ;
25 // Start the notepad.exe process.
26 Process1.start ();
27 }
28 Private Void Buttonstop_click ( Object Sender, eventargs E)
29 {
30 // Create an array of new process components and associate them with all process resources of the specified process name (Notepad.
31 Process [] myprocesses;
32 Myprocesses = process. getprocessesbyname ( " Notepad " );
33 Foreach (Process instance In Myprocesses)
34 {
35 // Set to wait 1000 milliseconds before terminating the current thread
36 Instance. waitforexit ( 1000 );
37 Instance. closemainwindow ();
38 }
39 }
40 Private Void Buttonview_click ( Object Sender, eventargs E)
41 {
42 Listbox1.items. Clear ();
43 // Create an array of the Process type and associate them with all processes in the system.
44 Process [] processes;
45 Processes = process. getprocesses ();
46 Foreach (PROCESS p In Processes)
47 {
48 // Idle indicates the name of the process that displays the CPU idle rate.
49 // The starttime of the access to idle is abnormal, so it is excluded.
50 If (P. processname! = " Idle " )
51 {
52 // Add each process name and process start time to listbox1
53 This . Listbox1.items. Add (
54 String . Format ( " {0,-30} {1: H: M: s} " , P. processname, P. starttime ));
55 }
56 }
57 }
58 }
59 }