C# 如何获取当前进程或线程的ID

这篇文章主要介绍了C# 如何获取当前进程或线程的ID,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

获取当前进程或线程的ID

如果获得当前进程的Id用:

Process[] processes = Process.GetProcesses(); 
foreach(Process process in processes) 
{ 
if(process.ProcessName == "进程名" 
{ 
MessageBox.Show(process.Id); 
} 
}
Process processes   =Process.GetCurrentProcess

processes.id获取当前进程的ID

如果获取当前线程的ID用:

Thread.CurrentThread.ManagedThreadId.ToString() (推荐)

AppDomain.GetCurrentThreadId().ToString()

C# 进程读取方法

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

GetProcesses()、Modules属性。

一些进程的属性信息:

  • Id // 进程唯一标识符 (Id)
  • Handle //关联进程的本机句柄 (Handle)
  • HandleCount //打开句柄数 (HandleCount)
  • BasePriority //关联进程的基本优先级
  • 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# 如何获取当前进程或线程的ID

基础教程推荐