using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Diagnostics; namespace SystemProcess{/// <summary>/// Form1 的摘要说明。/// </summary>public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.Button button1;private System.Windows.Forms.ListView listView1;private System.Windows.Forms.ColumnHeader columnHeader1;private System.Windows.Forms.ColumnHeader columnHeader2;private System.Windows.Forms.Button button2;/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();//// TODO: 在 InitializeComponent 调用后添加任何构造函数代码//}/// <summary>/// 清理所有正在使用的资源。/// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要使用代码编辑器修改/// 此方法的内容。/// </summary>private void InitializeComponent(){this.groupBox1 = new System.Windows.Forms.GroupBox();this.button1 = new System.Windows.Forms.Button();this.listView1 = new System.Windows.Forms.ListView();this.columnHeader1 = new System.Windows.Forms.ColumnHeader();this.columnHeader2 = new System.Windows.Forms.ColumnHeader();this.button2 = new System.Windows.Forms.Button();this.groupBox1.SuspendLayout();this.SuspendLayout();// // groupBox1// this.groupBox1.Controls.Add(this.button2);this.groupBox1.Controls.Add(this.listView1);this.groupBox1.Controls.Add(this.button1);this.groupBox1.Location = new System.Drawing.Point(8, 8);this.groupBox1.Name = "groupBox1";this.groupBox1.Size = new System.Drawing.Size(328, 168);this.groupBox1.TabIndex = 0;this.groupBox1.TabStop = false;this.groupBox1.Text = "系统进程列表";// // button1// this.button1.Location = new System.Drawing.Point(24, 136);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(128, 23);this.button1.TabIndex = 1;this.button1.Text = "更新系统进程信息";this.button1.Click = new System.EventHandler(this.button1_Click);// // listView1// this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {this.columnHeader1,this.columnHeader2});this.listView1.FullRowSelect = true;this.listView1.GridLines = true;this.listView1.Location = new System.Drawing.Point(16, 24);this.listView1.MultiSelect = false;this.listView1.Name = "listView1";this.listView1.Size = new System.Drawing.Size(296, 104);this.listView1.TabIndex = 2;this.listView1.View = System.Windows.Forms.View.Details;// // columnHeader1// this.columnHeader1.Text = "进程名";this.columnHeader1.Width = 100;// // columnHeader2// this.columnHeader2.Text = "程序窗口标题";this.columnHeader2.Width = 300;// // button2// this.button2.Location = new System.Drawing.Point(176, 136);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(128, 23);this.button2.TabIndex = 3;this.button2.Text = "关闭指定应用程序";this.button2.Click = new System.EventHandler(this.button2_Click);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(344, 182);this.Controls.Add(this.groupBox1);this.MaximizeBox = false;this.Name = "Form1";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "演示获取系统进程信息";this.Load = new System.EventHandler(this.Form1_Load);this.groupBox1.ResumeLayout(false);this.ResumeLayout(false);}#endregion/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main() {Application.Run(new Form1());}private void button1_Click(object sender, System.EventArgs e){//获取系统当前正在运行的应用程序this.listView1.Items.Clear();Process[] MyProcesses=Process.GetProcesses();int i=0;foreach(Process MyProcess in MyProcesses){//枚举系统进程if(MyProcess.MainWindowTitle.Length>1){//显示带窗口的应用程序string Title=MyProcess.MainWindowTitle;string []SubItems={MyProcess.ProcessName,Title};ListViewItem Item=new ListViewItem(SubItems);this.listView1.Items.Insert(i ,Item);}}}private void Form1_Load(object sender, System.EventArgs e){ button1_Click(null,null);}private void button2_Click(object sender, System.EventArgs e){//关闭指定应用程序 ListViewItem Item=this.listView1.SelectedItems[0];string ProcessName=Item.Text;string Title=Item.SubItems[1].Text;if(MessageBox.Show("是否确认需要关闭应用程序:" Title "?","信息提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No){return;}//取得指定进程Process[] MyProcess=Process.GetProcessesByName(ProcessName);MyProcess[0].EnableRaisingEvents=true;//添加进程退出事件代理MyProcess[0].Exited =new EventHandler(button1_Click);//如果进程没有关闭if(!MyProcess[0].HasExited){//如果关闭进程主窗口失败if(!MyProcess[0].CloseMainWindow()){if(MessageBox.Show("不能关闭应用程序窗口,需要继续终止进程吗?","信息提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes){try{//关闭进程MyProcess[0].Kill();}catch(Exception Err){MessageBox.Show("不能终止进程!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);}}}}else{MessageBox.Show("进程已经关闭!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);}}}}
评论