【问题标题】:.NET Process Monitor.NET 进程监视器
【发布时间】:2010-12-31 11:17:58
【问题描述】:

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

我可以使用以下内容来确定进程是否正在运行,但如果该进程已停止,则应用程序无法获取该进程。

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

【问题讨论】:

    标签: c# process system


    【解决方案1】:

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

    Audit process tracking

    How can I track what programs come and go on my machine?

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

    【讨论】:

      【解决方案2】:

      WMI 提供了一种方法来跟踪使用 Win32_ProcessTrace 类启动和终止的进程。最好用一个例子来说明。启动一个新的控制台应用程序,Project + Add Reference,选择 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);
        }
      }
      

      Edit the manifest 所以这个程序运行提升。然后只需启动一些程序来查看它的工作情况。请注意,它不是特别快。

      【讨论】:

      • 完美运行,但延迟很烦人,有没有办法让它更快?
      • 我刚试了一下,延迟不到 1 秒。我觉得还可以。
      • 太好了,我想等到命令行应用程序进程被初始化,但我不愿意向用户请求应用程序以 adm 身份运行
      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多