欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#进程监控方法实例分析

程序员文章站 2023-12-05 22:32:22
本文实例讲述了c#进程监控方法。分享给大家供大家参考。具体如下: using system; using system.collections.generic;...

本文实例讲述了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#程序设计有所帮助。