.NET进程监视器 [英] .NET Process Monitor
问题描述
有没有一种方法来确定当最后一个特定的时间机器最后一次运行一个进程?
Is there a way to determine when the last time a specific machine last ran a process?
我可以使用以下方法来确定是否一个进程正在运行,但如果它一直停止应用程序不能抢的过程。
I can use the following to determine if a process is running, but the application cannot grab the process if it has since stopped.
Process[] process = Process.GetProcessesByName(processName, serverName);
推荐答案
WMI提供了一种方法来跟踪开始并与Win32_ProcessTrace类终止进程。最佳示出的一个例子。启动一个新的控制台应用程序,项目+添加引用,选择System.Management。粘贴此code:
WMI provides a way to track processes starting and terminating with the Win32_ProcessTrace classes. Best shown with an example. Start a new Console application, Project + Add Reference, select System.Management. Paste this code:
using System;
using System.Management;
class Process {
public static void Main() {
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived);
startWatch.Start();
ManagementEventWatcher stopWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived);
stopWatch.Start();
Console.WriteLine("Press any key to exit");
while (!Console.KeyAvailable) System.Threading.Thread.Sleep(50);
startWatch.Stop();
stopWatch.Stop();
}
static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Process stopped: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) {
Console.WriteLine("Process started: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
}
编辑清单所以这个程序运行升高。然后,只需启动一些程序,看看它是怎么工作。当心这是不是特别快。
Edit the manifest so this program runs elevated. Then simply start some programs to see it at work. Beware that it is not especially quick.
这篇关于.NET进程监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!