目录获取当前进程或线程的IDC# 进程读取方法获取当前进程或线程的ID 如果获得当前进程的Id用: Process[] processes = Process.GetProcesse
如果获得当前进程的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()
首相要加入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());
}
}
}
输出效果:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: C# 如何获取当前进程或线程的ID
本文链接: https://lsjlt.com/news/146563.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0