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

C#进程的使用方法详解

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

关于C#进程的一些学习,包含了进程的各个方面的知识,欢迎阅读

 

C#进程学习,本机中的所有进程的监测与控制

转载请注明出处:http://www.cnblogs.com/minotmin/

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#进程的一些学习,包含了进程的各个方面的知识,欢迎阅读

 

C#进程学习,本机中的所有进程的监测与控制

转载请注明出处:http://www.cnblogs.com/minotmin/

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);
            }
        }
    }
}


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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