C#进程管理程序实现
运行效果图
部分代码如下:
1 #region 打开应用程序按钮事件处理程序 2 /// <summary> 3 /// 打开应用程序按钮事件处理程序 4 /// </summary> 5 /// <param name="sender"></param> 6 /// <param name="e"></param> 7 private void open_Click(object sender, EventArgs e) 8 { 9 OpenFileDialog fd = new OpenFileDialog(); 10 fd.FileName = ""; 11 fd.Filter = "应用程序(*.exe)|*.exe"; 12 fd.ShowDialog(); 13 if(fd.FileName!="") 14 { 15 //设置启动的信息 16 ProcessStartInfo ps = new ProcessStartInfo(fd.FileName); 17 temp = new Process(); 18 temp.StartInfo = ps; 19 temp.Start(); 20 Thread.Sleep(1000); 21 FreshDataGriedView(); 22 } 23 } 24 #endregion 25 26 27 #region 刷新进程表方法 28 /// <summary> 29 /// 刷新进程表 30 /// </summary> 31 private void FreshDataGriedView() { 32 Process[] _temp = Process.GetProcesses(); 33 dataGridView1.Rows.Clear(); 34 foreach(Process temp in _temp){ 35 int newRowIndex = dataGridView1.Rows.Add(); 36 DataGridViewRow newRow = dataGridView1.Rows[newRowIndex]; 37 newRow.Cells[0].Value=temp.Id; 38 newRow.Cells[1].Value=temp.ProcessName; 39 newRow.Cells[2].Value=string.Format("{0:###,##0.00}",temp.WorkingSet64/1024.0f/1024.0f); 40 try{ 41 newRow.Cells[3].Value=string.Format("{0}",temp.StartTime); 42 newRow.Cells[4].Value=temp.MainModule.FileName; 43 } 44 catch{ 45 newRow.Cells[3].Value=""; 46 newRow.Cells[4].Value=""; 47 } 48 } 49 50 } 51 #endregion 52 53 54 #region 刷新按钮的事件处理 55 /// <summary> 56 /// 刷新按钮的事件处理 57 /// </summary> 58 /// <param name="sender"></param> 59 /// <param name="e"></param> 60 private void fresh_but_Click(object sender, EventArgs e) 61 { 62 FreshDataGriedView(); 63 } 64 #endregion 65 66 67 #region 关闭进程按钮事件处理程序 68 /// <summary> 69 /// 关闭进程按钮事件处理程序 70 /// </summary> 71 /// <param name="sender"></param> 72 /// <param name="e"></param> 73 private void button1_Click(object sender, EventArgs e) 74 { 75 //label1.Text = process_selceted_id.ToString(); 76 try 77 { 78 //temp.Dispose(); 79 temp.CloseMainWindow(); 80 FreshDataGriedView(); 81 } 82 catch 83 { 84 MessageBox.Show("请选中一个进程","错误提示",MessageBoxButtons.OK); 85 } 86 } 87 #endregion 88 //行选中的事件 89 private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) 90 { 91 try 92 { 93 //label1.Text = e.RowIndex.ToString() + "--" + dataGridView1.Rows[e.RowIndex].Cells[0].Value; 94 process_selceted_id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value); 95 temp = Process.GetProcessById(process_selceted_id); 96 textbox_apand(); 97 98 } 99 catch { } 100 } 101 //显示进程信息 102 private void textbox_apand() { 103 StringBuilder stringText = new StringBuilder(); 104 stringText.AppendLine("进程ID:" + temp.Id); 105 stringText.AppendLine("进程ID:"+temp.ProcessName); 106 try 107 { 108 stringText.AppendLine(); 109 ProcessModule m = temp.MainModule; 110 stringText.AppendLine("文件名:"+m.FileName); 111 stringText.AppendLine("版 本:"+m.FileVersionInfo.FileVersion); 112 stringText.AppendLine("描 述:"+m.FileVersionInfo.FileDescription); 113 stringText.AppendLine("语 言:"+m.FileVersionInfo.Language); 114 115 } 116 catch{ 117 stringText.AppendLine("无法获取信息"); 118 } 119 this.textBox1.Text = stringText.ToString(); 120 }
人生哪有不疯狂!