项目需求,最近做一个WINFORMS的项目,需求里面要求打印出本机所有进程,以及查看单独进程信息和进程模块信息,还要通过程序来打开系统中的进程!其实项目是一个金融方面的管理项目,不知道本人做的对大家有没有什么用处!
1.显示全部进程
2.显示单个进程
3.显示进程线程集合时间跟优先级别
4.显示进程的模块信息
5.打开一个系统中的进程
以下个人代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyJinCheng
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//打印出本机进程
public void ListAllRunningProcess()
{
Process[] proc = Process.GetProcesses(".");
foreach (Process p in proc)
{
string info = string.Format("->进程ID:{0}\t进程名字:{1}",p.Id,p.ProcessName);
this.listBox1.Items.Add(info);
}
}
private void Form1_Load(object sender, EventArgs e)
{
ListAllRunningProcess();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = e.KeyChar < '0' || e.KeyChar > '9';
if (e.KeyChar == (char)8)
{
e.Handled = false;
MessageBox.Show("输入的字符不符合规定","错误");
}
}
//显示单个正在运行的进程信息
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Length == 0)
{
MessageBox.Show("请输入您要查询的进程ID", "空值类型");
}
else
{
this.listBox1.Items.Clear();
int PID = Convert.ToInt32(this.textBox1.Text);
try
{
Process proc;
proc = Process.GetProcessById(PID);
this.listBox1.Items.Add(proc.ToString());
}
catch
{
MessageBox.Show("没有你要查看的进程", "错误");
}
}
}
//查看单个进程线程集合
private void button2_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Length == 0)
{
MessageBox.Show("请输入您要查询的进程ID", "空值类型");
}
else
{
this.listBox1.Items.Clear();
int PID = Convert.ToInt32(this.textBox1.Text);
Process proc;
try
{
proc = Process.GetProcessById(PID);
}
catch
{
MessageBox.Show("没有你要查看的进程线程集合!", "错误");
return;
}
this.listBox1.Items.Add("进程名:'" + proc.ProcessName + "'");
ProcessThreadCollection threads = proc.Threads;
foreach (ProcessThread p in threads)
{
string info = string.Format("->线程ID:{0}\t开始时间:{1}\t优先级别:{2}", p.Id, p.StartTime, p.PriorityLevel);
this.listBox1.Items.Add(info);
}
}
}
//查看进程模块信息
private void button3_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Length == 0)
{
MessageBox.Show("请输入您要查询的进程ID", "空值类型");
}
else
{
this.listBox1.Items.Clear();
int PID = Convert.ToInt32(this.textBox1.Text);
Process proc;
try
{
proc = Process.GetProcessById(PID);
}
catch
{
MessageBox.Show("没有您要查询的进程模块信息", "错误");
return;
}
this.listBox1.Items.Add("模块话进程:'" + proc.ProcessName + "'");
try
{
ProcessModuleCollection module = proc.Modules;
foreach (ProcessModule m in module)
{
string info = string.Format("模块名字:{0}\t模块文件的完整路径:{1}\t模块的内存地址:{2}", m.ModuleName, m.FileName, m.BaseAddress);
this.listBox1.Items.Add(info);
}
}
catch
{
MessageBox.Show("没有模块信息", "错误");
}
}
}
private void button4_Click(object sender, EventArgs e)
{
Open op = new Open();
op.Show();
}
}
}
打开一个进程的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyJinCheng
{
public partial class Open : Form
{
public Open()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string Name = this.textBox1.Text;
try
{
Process proc;
proc = Process.Start(Name);
}
catch
{
MessageBox.Show("没有你要打开的进程!", "输入错误");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Opacity < 1)
{
this.Opacity = this.Opacity + 0.05;//随时间增加Opacity的值,每次增加0.05
}
else
{
this.timer1.Enabled = false;
}
}
private void Open_Load(object sender, EventArgs e)
{
this.timer1.Enabled = true;//使时间控件生效
this.Opacity = 0;//设置初始透明度 Opacity:0-1.0, 0为全透明,1.0为不透明
}
}
}
不知道需求为什么会有这个不过做的很开心与大家分享一下!