C#编程应用--进程管理

一年多没接触C#开发了,复习一下Winform方面的知识:

 

 C#开启、关闭进程,获取进程列表;

 

有图有真相:

C#编程应用--进程管理 

 

 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              // 启动Notepad.exe进程.
26              process1.Start();
27         }
28          private  void buttonStop_Click( object sender, EventArgs e)
29         {
30              // 创建新的Process组件的数组,并将它们与指定的进程名称(Notepad)的所有进程资源相关联.
31              Process[] myprocesses;
32             myprocesses = Process.GetProcessesByName( " Notepad ");
33              foreach (Process instance  in myprocesses)
34             {
35                  // 设置终止当前线程前等待1000毫秒
36                  instance.WaitForExit( 1000);
37                 instance.CloseMainWindow();
38             }
39         }
40          private  void buttonView_Click( object sender, EventArgs e)
41         {
42             listBox1.Items.Clear();
43              // 创建Process类型的数组,并将它们与系统内所有进程相关联
44              Process[] processes;
45             processes = Process.GetProcesses();
46              foreach (Process p  in processes)
47             {
48                  // Idle指显示CPU空闲率的进程名称
49                   // 由于访问Idle的StartTime会出现异常,所以将其排除在外
50                   if (p.ProcessName !=  " Idle ")
51                 {
52                      // 将每个进程名和进程开始时间加入listBox1中
53                       this.listBox1.Items.Add(
54                      string.Format( " {0,-30}{1:h:m:s} ", p.ProcessName, p.StartTime));
55                 }
56             }
57         }
58     }
59 }

 

你可能感兴趣的:(C#)