如何获取最后运行的进程
本文关键字:最后 运行 进程 获取 何获取 | 更新日期: 2023-09-27 18:14:35
Process[] processList1 = Process.GetProcesses();
int size = processList1.Length;
MessageBox.Show("" + processList1[size - 1]);
如果您正在谈论最近的进程,您可以通过按开始时间排序列表来实现:
Process mostRecent = Process.GetProcesses().OrderBy(p => p.StartTime).Last();
试试这个:
public partial class Form1 : Form
{
Process[] processList1;
public Form1()
{
InitializeComponent();
processList1 = Process.GetProcesses();
}
//set timer1.Interval to 5000
private void timer1_Tick(object sender, EventArgs e)
{
foreach (Process item in Process.GetProcesses())
{
if (!processList1.Contains(item))
{
//get all new Processes
}
}
processList1 = Process.GetProcesses();
}
}
使用此代码。这段代码获取所有进程并比较开始日期,如果您打开一个新进程,它将在表单中的label1中显示它的名称。
private void timer1_Tick(object sender, EventArgs e)
{
System.Diagnostics.Process[] processList1 = System.Diagnostics.Process.GetProcesses();
DateTime min = processList1[0].StartTime;
Process last = new Process();
foreach (Process a in processList1)
{
try
{
if (DateTime.Compare(a.StartTime, min) >= 0)
{
last = a;
min = a.StartTime;
}
}
catch (Exception)
{
}
}
label1.Text = last.ProcessName;
}