程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#過程監控辦法實例剖析

C#過程監控辦法實例剖析

編輯:C#入門知識

C#過程監控辦法實例剖析。本站提示廣大學習愛好者:(C#過程監控辦法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#過程監控辦法實例剖析正文


本文實例講述了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;
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);
    //有些過程沒法獲得啟動時光和文件名信息,所以要用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("過程稱號:" + p.ProcessName + ", ID:" + p.Id);
   try
   {
    sb.AppendLine("過程優先級:" + p.BasePriority + "(優先級種別: " + 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("挪用的模塊(.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("產生異常,緣由是:" + ex.Message);
   }
  }
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved