您的位置:首页 > 编程语言 > C#

C#进程管理程序实现

2013-11-14 22:26 495 查看
运行效果图



    部分代码如下:

#region  打开应用程序按钮事件处理程序
/// <summary>
/// 打开应用程序按钮事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void open_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.FileName = "";
fd.Filter = "应用程序(*.exe)|*.exe";
fd.ShowDialog();
if(fd.FileName!="")
{
//设置启动的信息
ProcessStartInfo ps = new ProcessStartInfo(fd.FileName);
temp = new Process();
temp.StartInfo = ps;
temp.Start();
Thread.Sleep(1000);
FreshDataGriedView();
}
}
#endregion

#region  刷新进程表方法
/// <summary>
///  刷新进程表
/// </summary>
private void FreshDataGriedView() {
Process[] _temp = Process.GetProcesses();
dataGridView1.Rows.Clear();
foreach(Process temp in _temp){
int newRowIndex = dataGridView1.Rows.Add();
DataGridViewRow newRow = dataGridView1.Rows[newRowIndex];
newRow.Cells[0].Value=temp.Id;
newRow.Cells[1].Value=temp.ProcessName;
newRow.Cells[2].Value=string.Format("{0:###,##0.00}",temp.WorkingSet64/1024.0f/1024.0f);
try{
newRow.Cells[3].Value=string.Format("{0}",temp.StartTime);
newRow.Cells[4].Value=temp.MainModule.FileName;
}
catch{
newRow.Cells[3].Value="";
newRow.Cells[4].Value="";
}
}

}
#endregion

#region  刷新按钮的事件处理
/// <summary>
/// 刷新按钮的事件处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void fresh_but_Click(object sender, EventArgs e)
{
FreshDataGriedView();
}
#endregion

#region  关闭进程按钮事件处理程序
/// <summary>
/// 关闭进程按钮事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//label1.Text = process_selceted_id.ToString();
try
{
//temp.Dispose();
temp.CloseMainWindow();
FreshDataGriedView();
}
catch
{
MessageBox.Show("请选中一个进程","错误提示",MessageBoxButtons.OK);
}
}
#endregion
//行选中的事件
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
//label1.Text = e.RowIndex.ToString() + "--" + dataGridView1.Rows[e.RowIndex].Cells[0].Value;
process_selceted_id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
temp = Process.GetProcessById(process_selceted_id);
textbox_apand();

}
catch { }
}
//显示进程信息
private void textbox_apand() {
StringBuilder stringText = new StringBuilder();
stringText.AppendLine("进程ID:" + temp.Id);
stringText.AppendLine("进程ID:"+temp.ProcessName);
try
{
stringText.AppendLine();
ProcessModule m = temp.MainModule;
stringText.AppendLine("文件名:"+m.FileName);
stringText.AppendLine("版  本:"+m.FileVersionInfo.FileVersion);
stringText.AppendLine("描  述:"+m.FileVersionInfo.FileDescription);
stringText.AppendLine("语  言:"+m.FileVersionInfo.Language);

}
catch{
stringText.AppendLine("无法获取信息");
}
this.textBox1.Text = stringText.ToString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: