C# 进程读取方法

首相要加入using System.Diagnostics;命名空间。

GetProcesses()Modules属性。一些进程的属性信息:

  1. Id // 进程唯一标识符 (Id)
  2. Handle //关联进程的本机句柄 (Handle)
  3. HandleCount //打开句柄数 (HandleCount)
  4. BasePriority //关联进程的基本优先级
  5. StartTime //进程启动时间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;//包含进程相关

namespace ProccessEx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Process[] processes = null;

        private void btnGetProcessess_Click(object sender, EventArgs e)
        {
            processes = Process.GetProcesses();// 获取本地计算机上的进程

            this.lbProcess.Items.Clear();
            foreach (Process item in processes) {
                this.lbProcess.Items.Add(item.ProcessName);
            }
        }

        private void lbProcess_Click(object sender, EventArgs e)
        {

            if (lbProcess.Items.Count == 0) return;
            else {
                lbModels.Items.Clear();
            }
            Process currentProcess = processes[lbProcess.SelectedIndex]; // SelectedIndex 属性
            try {
                ProcessModuleCollection modules = currentProcess.Modules; // Mpdules 属性
                foreach (ProcessModule item in modules) {  // 注意是 ProcessModule 类型
                    lbModels.Items.Add(item.FileName);
                }
                // 显示当前进程的基本信息
                string info = "进程唯一标识符 (Id):" + currentProcess.Id + "\r\n\r\n";
                info += "关联进程的本机句柄 (Handle):" + currentProcess.Handle + "\r\n\r\n";
                info += "打开句柄数 (HandleCount):" + currentProcess.HandleCount + "\r\n\r\n";
                info += "关联进程的基本优先级:" + currentProcess.BasePriority + "\r\n\r\n";
                info += "进程启动时间:" + currentProcess.StartTime + "\r\n\r\n";

                txtProcessInfo.Text = info;
            }
            catch (Exception ex) {

                MessageBox.Show(ex.Message);
            }
        }

        private void btnKill_Click(object sender, EventArgs e)
        {
            if (lbProcess.Items.Count == 0 || lbProcess.SelectedIndex == -1) return;

            Process currentProccess = processes[lbProcess.SelectedIndex];// 获取当前鼠标选择的进程
            currentProccess.Kill();// 杀掉进程
            currentProccess.Close();// 释放进程资源

            lbProcess.Items.RemoveAt(lbProcess.SelectedIndex);

            lbModels.Items.Clear();
            txtProcessInfo.Clear();
        }

        /// <summary>
        /// 用360浏览器打开我的博客
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenIE_Click(object sender, EventArgs e)
        {
            // 没注册就使用绝对路径
            // 打开我的博客
            Process.Start(@"C:\Users\Administrator\AppData\Roaming\360se6\Application\360se.exe", "blog.huvjie.com");
        }
        /// <summary>
        /// 打开新进程,如:notepad
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartnew_Click(object sender, EventArgs e)
        {
            Process.Start(txtProcessFileName.Text.Trim());
        }
    }
}

输出效果:

C# 进程读取方法

上一篇:如何用PHP在sftp-server上编写文件?


下一篇:08-购物车小案例