1.獲取本機的 string processName;//進程名成 int processID;//ID int threadNumber;//進程數 TimeSpan cpuTime;//CPU時間 long processMemory;//內存數量 2.要實現的框架結構(添加三個菜單和一個listView),listView的屬性設置見上篇C#中控件的應用: 為三個菜單添加事件處理函數,分別添加代碼: private void 創建進程ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofDlg = new OpenFileDialog();//構建打開文件對話框 ofDlg.Filter = "All Executables(*.exe)|*.exe";//篩選 if (ofDlg.ShowDialog() == DialogResult.OK) { try { Process newprocess = Process.Start(ofDlg.FileName);//選擇進程 } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } private void 刷新列表ToolStripMenuItem_Click(object sender, EventArgs e) {//顯示進程信息 string processName;//進程名成 int processID;//ID int threadNumber;//進程數 TimeSpan cpuTime;//CPU時間 long processMemory;//內存數量 listView1.Items.Clear();//清除列表視圖中原有的內容 Process[] processes = Process.GetProcesses();//定義動態數組,獲得當前系統中的所有進程 try { //在列表中加入每個進程項目 for (int i = 0; i < processes.Length; i++) { processName = processes[i].ProcessName; processID = processes[i].Id; threadNumber = processes[i].Threads.Count;//獲得進程的數量 cpuTime = processes[i].TotalProcessorTime; processMemory = processes[i].WorkingSet64; string[] subItems = { processName, processID.ToString(), threadNumber.ToString(), string.Format("{0:00}:{1:00}:{2:00}", cpuTime.Hours,cpuTime.Minutes,cpuTime.Seconds), string.Format("{0:#,#,#}k",processMemory/1024) }; //插入一行項目 listView1.Items.Insert(i, new ListViewItem(subItems)); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } private void 結束進程ToolStripMenuItem_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { string str = listView1.SelectedItems[0].SubItems[1].Text; int processID = Int32.Parse(str);//獲取選中項目的ID 值 //根據ID獲取進程 Process selectedProcess = Process.GetProcessById(processID); selectedProcess.Kill();//終止進程 } }