• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#Winform进程信息的获取

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

获取进程信息
1. 如何获取进程信息
(1) 获取本地计算机的所有进程
Process [] myProcess = Process.GetProcesses();
(2) 获取本地计算机上指定名称的进程
Process [] myProcess = Process.GetProcessesByName(“进程名称”);
注意:(a)进程名称不带扩展名。
     (b)可以是任何一个可执行文件
例如:Process [] myPrecess = Process.GetProcessesByName(“Notopad++”);
(3) 获取远程计算机的所有进程:
Process [] myProcess = Process.GetProcesses(remoteMachineName);
例如:
Process [] myProcesses = Process.GetProcesses(“192.168.0.1”);
(4) 获取远程计算机上指定名称的进程:
Process [] myProcess
= Process.GetProcessesByName(“远程进程名称”,remoteMachineName);

下面写一个winform程序来举例说明:
要求:(1)用DataGridView显示本地计算机所有进程信息
     (2)鼠标单击某一行时,显示该行进程的详细信息

 


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 WindowsFormsApplication
{
    public partial class ProcessForm : Form
    {
        public ProcessForm()
        {
            InitializeComponent();

            GetAllProcess();
        }

        private void GetAllProcess()
        {
            //创建进程对象获取本地计算机的所有进程
            Process[] myProcess = Process.GetProcesses();

            this.dataGridView.AutoResizeColumns();//自动调整单元格的宽度来适应其内容
            this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

            List<ProcessModel> proList = new List<ProcessModel>();
            foreach (Process ps in myProcess)
            {
                ProcessModel pm = new ProcessModel();
                pm.ProcessID = ps.Id;
                pm.ProcessName = ps.ProcessName;
                pm.PhysicalMemory = string.Format("{0:###,##0.00}MB", ps.WorkingSet64 / 1024.0f / 1024.0f);

                try
                {
                    //有的不提供运行时间
                    pm.StartTime = string.Format("{0}", ps.StartTime);
                }
                catch (Exception)
                {
                    pm.StartTime = "";
                }
                try
                {
                    //有些不能获取到
                    pm.FilesName = ps.MainModule.FileName;
                }
                catch (Exception)
                {
                    pm.FilesName = "";
                }
                proList.Add(pm);

            }
            dataGridView.DataSource = proList;
            SetDgv(this.dataGridView);
        }
        //设置dataGridView的显示和DataSource的属性名字要一致
        public void SetDgv(DataGridView dgv)
        {
            dgv.AutoGenerateColumns = false;//不自动创建列
            dgv.Columns[0].HeaderText = "进程ID";
            dgv.Columns[0].DataPropertyName = "ProcessID";
            dgv.Columns[1].HeaderText = "进程名称";
            dgv.Columns[1].DataPropertyName = "ProcessName";
            dgv.Columns[2].HeaderText = "物理内存";
            dgv.Columns[2].DataPropertyName = "PhysicalMemory";
            dgv.Columns[3].HeaderText = "运行时间";
            dgv.Columns[3].DataPropertyName = "StartTime";
            dgv.Columns[4].HeaderText = "文件名";
            dgv.Columns[4].DataPropertyName = "FilesName";
        }

        private void ShowProcessInfo(Process p)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("进程ID:{0},进程名称:{1}", p.Id, p.ProcessName);
            try
            {
                sb.AppendLine("进程优先级:" + p.BasePriority + "(优先级类别:" + p.PriorityClass + ")");

                sb.AppendLine("文件名:" + p.MainModule.FileName);
                sb.AppendLine("版本:" + p.MainModule.FileVersionInfo.FileVersion);
                sb.AppendLine("描述:" + p.MainModule.FileVersionInfo.FileDescription);
                sb.AppendLine("语言:" + p.MainModule.FileVersionInfo.Language);
                sb.AppendLine(".......................................");
                if (p.Modules != null)
                {
                    //获取跟相应进程相关联的模块
                    ProcessModuleCollection pmc = p.Modules;
                    sb.AppendLine("调用的模块(.dll)");
                    for (int i = 0; i < pmc.Count; i++)
                    {
                        sb.AppendLine("模块名:" + pmc[i].ModuleName +"\t" +
                                      "版本:" + pmc[i].FileVersionInfo.FileVersion +"\t"+
                                      "描述:" + pmc[i].FileVersionInfo.FileDescription);
                    }
                }
            }
            catch (Exception)
            {
                sb.AppendLine("其他信息:无法获取!");
            }
            this.richTextBox.Text = sb.ToString();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetAllProcess();
        }

        private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dataGridView.Rows.Count > 0)
            {
                dataGridView.Rows[e.RowIndex].Selected = true;
                int processID = (int)dataGridView.CurrentRow.Cells[0].Value;
                //根据ID获取进程
                Process  pro = Process.GetProcessById(processID);
                ShowProcessInfo(pro);
            }
        }


    }
}

 

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsFormsApplication
{
   public class ProcessModel
    {
       public ProcessModel() { }
       public int ProcessID { get; set; }
       public string ProcessName { get; set; }
       public string PhysicalMemory { get; set; }
       public string StartTime { get; set; }
       public string FilesName { get; set; }
    }
}

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#dll作为文件资源嵌入到Resource发布时间:2022-07-10
下一篇:
*C#(WPF)--矩阵拖动和矩阵动画(拖动展开,不足动画效果)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap