C#進(jìn)程監(jiān)控方法實例分析
更新時間:2015年07月02日 16:25:31 作者:程序猴
這篇文章主要介紹了C#進(jìn)程監(jiān)控方法,以實例形式較為詳細(xì)的分析了C#針對進(jìn)程的讀取及操作技巧,需要的朋友可以參考下
本文實例講述了C#進(jìn)程監(jiān)控方法。分享給大家供大家參考。具體如下:
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 ProcessMonitor { public partial class Form1 : Form { Process[] myProcess; public Form1() { InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.AutoResizeColumns(); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.MultiSelect = false; } private void Form1_Load(object sender, EventArgs e) { GetAllProcess(); } private void GetAllProcess() { dataGridView1.Rows.Clear(); myProcess = Process.GetProcesses(); foreach (Process p in myProcess) { int newRowIndex = dataGridView1.Rows.Add(); DataGridViewRow row = dataGridView1.Rows[newRowIndex]; row.Cells[0].Value = p.Id; row.Cells[1].Value = p.ProcessName; row.Cells[2].Value = string.Format("{0:###,##0.00}MB", p.WorkingSet64 / 1024.0f / 1024.0f); //有些進(jìn)程無法獲取啟動時間和文件名信息,所以要用try/catch try { row.Cells[3].Value = string.Format("{0}", p.StartTime); row.Cells[4].Value = p.MainModule.FileName; } catch { row.Cells[3].Value = ""; row.Cells[4].Value = ""; } } } private void ShowProcessInfo(Process p) { StringBuilder sb = new StringBuilder(); sb.AppendLine("進(jìn)程名稱:" + p.ProcessName + ", ID:" + p.Id); try { sb.AppendLine("進(jìn)程優(yōu)先級:" + p.BasePriority + "(優(yōu)先級類別: " + p.PriorityClass + ")"); ProcessModule m = p.MainModule; sb.AppendLine("文件名:" + m.FileName); sb.AppendLine("版本:" + m.FileVersionInfo.FileVersion); sb.AppendLine("描述:" + m.FileVersionInfo.FileDescription); sb.AppendLine("語言:" + m.FileVersionInfo.Language); sb.AppendLine("------------------------"); if (p.Modules != null) { ProcessModuleCollection pmc = p.Modules; sb.AppendLine("調(diào)用的模塊(.dll):"); for (int i = 1; i < pmc.Count; i++) { sb.AppendLine( "模塊名:" + pmc[i].ModuleName + "\t" + "版本:" + pmc[i].FileVersionInfo.FileVersion + "\t" + "描述:" + pmc[i].FileVersionInfo.FileDescription); } } } catch { sb.AppendLine("其他信息:無法獲取"); } this.richTextBox1.Text = sb.ToString(); } private void buttonRefresh_Click(object sender, EventArgs e) { GetAllProcess(); } private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { //DataGridView.HitTestInfo h = dataGridView1.HitTest(e.X, e.Y); //if (h.Type== DataGridViewHitTestType.Cell || h.Type == DataGridViewHitTestType.RowHeader) //{ // dataGridView1.Rows[h.RowIndex].Selected = true; // int processeId = (int)dataGridView1.CurrentRow.Cells[0].Value; // ShowProcessInfo(Process.GetProcessById(processeId)); //} } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { try { if (e.RowIndex >= 0) { int processId = (int)dataGridView1.Rows[e.RowIndex].Cells[0].Value; ShowProcessInfo(Process.GetProcessById(processId)); } } catch(Exception ex) { MessageBox.Show("發(fā)生異常,原因是:" + ex.Message); } } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
您可能感興趣的文章:
- 關(guān)于.NET/C#/WCF/WPF 打造IP網(wǎng)絡(luò)智能視頻監(jiān)控系統(tǒng)的介紹
- C#如何實現(xiàn)監(jiān)控手機屏幕(附源碼下載)
- C# FileSystemWatcher 在監(jiān)控文件夾和文件時的使用方法
- C# 監(jiān)控 Windows 文件夾的方法
- C#使用FileSystemWatcher控件實現(xiàn)的文件監(jiān)控功能示例
- C#利用性能計數(shù)器監(jiān)控網(wǎng)絡(luò)狀態(tài)
- C#獲取串口列表實現(xiàn)實時監(jiān)控串口
- C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- C#實現(xiàn)軟件監(jiān)控外部程序運行狀態(tài)的方法
- c#使用filesystemwatcher實時監(jiān)控文件目錄的添加和刪除
- C# 實現(xiàn)視頻監(jiān)控系統(tǒng)(附源碼)

DevExpress實現(xiàn)自定義TreeListNode的Tooltip的方法
這篇文章主要介紹了DevExpress實現(xiàn)自定義TreeListNode的Tooltip的方法,需要的朋友可以參考下
2014-08-08