C#进程操作
一些常规的操作,附上代码,相信你能看懂。
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;
using System.IO;
namespace Windows
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0)
{
MessageBox.Show("请输入你查询的内容", "空值提示");
}
else
{
textBox1.Text = "";
int pid = Convert.ToInt32(textBox2.Text);
try
{
Process proc;
proc = Process.GetProcessById(pid);//跟据进程ID获得进程
textBox1.Text=proc.ToString()+"\r\n";
}
catch
{
MessageBox.Show("没有你要查看的进程","错误");
}
}
}
public void processall()
{
Process[] proc = Process.GetProcesses("netmaster");
foreach (Process p in proc)
{
string info = string.Format("->进程ID:{0}\t进程名字:{1}", p.Id, p.ProcessName);
this.textBox1.Text += info + "\r\n";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length == 0)
{
MessageBox.Show("请输入你查询的内容", "空值提示");
}
else
{
textBox1.Text = "";
int pid = Convert.ToInt32(textBox2.Text);
Process proc;
try
{
proc = Process.GetProcessById(pid);
textBox1.Text += "模块话进程:'" + proc.ProcessName + "'\r\n";
}
catch
{
MessageBox.Show("没有你要查看的进程", "错误");
return;
}
try
{
ProcessModuleCollection module = proc.Modules;
foreach (ProcessModule m in module)
{
string info = string.Format("模块名字:{0}\r\n模块文件的完整路径:{1}\r\n模块的内存地址:{2}", m.ModuleName, m.FileName, m.BaseAddress);
textBox1.Text += info + "\r\n";
}
}
catch
{
MessageBox.Show("没有模块信息", "错误");
return;
}
}
}
private void Form2_Load(object sender, EventArgs e)
{
processall();
}
private void textBox2_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 button2_Click(object sender, EventArgs e)
{
if (this.textBox2.Text.Length == 0)
{
MessageBox.Show("请输入您要查询的进程ID", "空值类型"); ;
}
else
{
textBox1.Text = "";
int pid = Convert.ToInt32(textBox2.Text);
Process proc;
try
{
proc = Process.GetProcessById(pid);
}
catch
{
MessageBox.Show("没有你要查看的进程纯种集合", "错误");
return;
}
try
{
this.textBox1.Text = "进程名:'" + 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);
textBox1.Text += info + "\r\n";
}
}
catch
{
MessageBox.Show("系统进程!", "系统错误");
}
}
}
private void button4_Click(object sender, EventArgs e)
{
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
string name = textBox2.Text;
try
{
textBox2.Text = "";
Process proc;
proc = Process.Start(name);
textBox2.Text = proc.Id.ToString();
}
catch
{
MessageBox.Show("没有你要打开的进程!", "输入错误");
}
}
private void button5_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
openFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*|可执行文件(*.exe)|*.exe";
DialogResult db = openFileDialog1.ShowDialog();
if (db == DialogResult.OK)
{
string str = openFileDialog1.FileName;
textBox2.Text = str;
}
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
}
}
posted on 2012-09-24 00:51 程序猴chengxuhou.com 阅读(786) 评论(0) 编辑 收藏 举报