C# 进程监控器

学习《C#网络应用编程》的一个开始,在博客上记录下源码以便日后翻用。

 

引入命名空间:

using System.Diagnostics;


然后直接贴源码啦

namespace ProcessMonitor
{
    public partial class Form1 : Form
    {
        Process[] myProcess;
        public Form1()
        {
            InitializeComponent();
            dgv_Process.AutoResizeColumns();
        }

        private void btn_Refurbish_Click(object sender, EventArgs e)
        {
            GetAllProcessToControl();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetAllProcessToControl();
        }

        /// <summary>
        /// 获取全部进程并填充到dgv_Process控件中
        /// </summary>
        private void GetAllProcessToControl()
        {
            dgv_Process.Rows.Clear();
            myProcess = Process.GetProcesses();
            foreach (Process p in myProcess)
            {
                    int newRowIndex = dgv_Process.Rows.Add();
                    DataGridViewRow row = dgv_Process.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 (Exception)
                {
                    row.Cells[3].Value = "";
                    row.Cells[4].Value = "";
                    //throw;
                }
            }
        }

        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 (Exception)
            {
                sb.AppendLine("其他信息:无法获取");
                //throw;
            }
            this.txt_ProcessInfo.Text = sb.ToString();
        }

        private void dgv_Process_MouseClick(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo h = dgv_Process.HitTest(e.X, e.Y);
            if (h.Type == DataGridViewHitTestType.Cell || h.Type == DataGridViewHitTestType.RowHeader)
            {
                dgv_Process.Rows[h.RowIndex].Selected = true;
                int processId = (int)dgv_Process.CurrentRow.Cells[0].Value;
                ShowProcessInfo(Process.GetProcessById(processId));
            }
        }
    }
}


 

界面截图:

 

主要控件:

DataGridView

TextBox


参考:http://www.wxzzz.com/Program/ProcessMonitoring

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Andrew_wx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值