Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET进程监视器_C#_Process_System - Fatal编程技术网

C# .NET进程监视器

C# .NET进程监视器,c#,process,system,C#,Process,System,有没有办法确定特定机器上次运行进程的时间 我可以使用以下方法来确定进程是否正在运行,但如果进程已经停止,应用程序将无法捕获该进程 Process[] process = Process.GetProcessesByName(processName, serverName); 使用进程类将无法执行此操作。但是,通过在Windows中配置审核进程跟踪,应该可以确定应用程序上次运行的时间。以下链接可能会帮助您开始: 进程跟踪将在Windows事件日志中创建条目,然后您可以使用C#访问这些条目。W

有没有办法确定特定机器上次运行进程的时间

我可以使用以下方法来确定进程是否正在运行,但如果进程已经停止,应用程序将无法捕获该进程

Process[] process = Process.GetProcessesByName(processName, serverName);

使用
进程
类将无法执行此操作。但是,通过在Windows中配置审核进程跟踪,应该可以确定应用程序上次运行的时间。以下链接可能会帮助您开始:


进程跟踪将在Windows事件日志中创建条目,然后您可以使用C#访问这些条目。

WMI提供了一种跟踪使用Win32#U ProcessTrace类启动和终止的进程的方法。最好用一个例子来说明。启动新的控制台应用程序,项目+添加引用,选择System.Management。粘贴此代码:

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);
  }
}

所以这个程序运行得很高。然后简单地启动一些程序,看看它在工作。注意它不是特别快。

它工作得很好,但是延迟很烦人,有没有办法让它更快?我刚试过,延迟不到1秒。我认为这没问题。这很好,我想等到命令行应用程序进程被初始化,但我不想请求用户将应用程序作为adm运行