📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics ;
namespace ControlProcessApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ListView lvProcess;
private System.ComponentModel.IContainer components;
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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.lvProcess = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(312, 38);
this.toolBar1.TabIndex = 1;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.Text = "新建进程";
//
// toolBarButton3
//
this.toolBarButton3.Text = "退出程序";
//
// openFileDialog1
//
this.openFileDialog1.Filter = "可执行文件(*.exe)|*.exe";
//
// lvProcess
//
this.lvProcess.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader4});
this.lvProcess.Dock = System.Windows.Forms.DockStyle.Fill;
this.lvProcess.Location = new System.Drawing.Point(0, 38);
this.lvProcess.MultiSelect = false;
this.lvProcess.Name = "lvProcess";
this.lvProcess.Size = new System.Drawing.Size(312, 223);
this.lvProcess.TabIndex = 2;
this.lvProcess.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "名称";
//
// columnHeader2
//
this.columnHeader2.Text = "PID";
//
// columnHeader3
//
this.columnHeader3.Text = "CPU时间";
//
// columnHeader4
//
this.columnHeader4.Text = "占用内存";
//
// toolBarButton2
//
this.toolBarButton2.Text = "结束进程";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(312, 261);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lvProcess,
this.toolBar1});
this.Name = "Form1";
this.Text = "Process Manager";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch (toolBar1.Buttons .IndexOf (e.Button ))
{
case 0: //新建进程
MyStartProcess();
break;
case 1: //停止进程
MyStopProcess();
break;
case 2: //退出程序
MyQuit();
break;
}
}
private void MyStartProcess()
{ //打开可执行文件
if ((openFileDialog1.ShowDialog()==DialogResult.OK)&&(openFileDialog1.FileName != null))
{
//使用Process.Start()方法创建进程
Process newp = Process.Start (openFileDialog1.FileName );
if (newp!= null)
{
//使用Process类的属性,获得各项进程数据
string[] subItem =
{
newp.ProcessName , //进程名字
newp.Id.ToString(), //进程号
newp.TotalProcessorTime.ToString (), //占用处理器时间
String.Format ("{0:N}K",newp.WorkingSet/1024)//占用物理内存
};
//向ListView中添加进程数据
listView1.Items .Add(new ListViewItem(subItem));
}
}
}
private void MyStopProcess()
{
if (listView1.SelectedItems .Count >0)
{ //停止选定的进程,删除所在项
MyListDel(listView1.SelectedItems [0]);
}
}
private void MyQuit()
{ //停止列表中所有进程,然后退出
foreach (ListViewItem item in listView1.Items )
{
MyListDel(item);
}
Application.Exit ();
}
private void MyListDel(ListViewItem item)
{
int Pid = int.Parse (item.SubItems [1].Text);
//由进程id获得进程
Process p = Process.GetProcessById (Pid);
try
{ //关闭进程主界面
p.CloseMainWindow();
}
catch(Exception ex)
{
if (MessageBox.Show (ex.Message ,"是否立即停止?",MessageBoxButtons.YesNo )==DialogResult.Yes )
{ //立即关闭进程
p.Kill ();
}
}
finally
{ //删除列表中的项
listView1.Items .Remove (item);
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{ //定时更新进程列表
MyListRefresh();
}
private void MyListRefresh()
{ //检查列表中每一个进程
foreach (ListViewItem item in listView1.Items )
{
int Pid = int.Parse (item.SubItems [1].Text);
try
{ //试图由进程id得到进程的引用
Process p = Process.GetProcessById (Pid);
item.SubItems[2].Text = p.TotalProcessorTime .ToString ();
item.SubItems [3].Text = String.Format ("{0:N}K",p.WorkingSet/1024);
}
catch{
//如果进程不存在,则删除对应项
listView1.Items .Remove (item);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -