显示器时推出一个exe显示器、exe

2023-09-02 10:38:23 作者:烟柳

我有一个应用程序需要以运行某些应用程序的功能工作的一些服务。我想启用该选项只启动外部Windows服务来初始化应用程序启动后。 (而不是让你的设备自动启动,占用内存当应用程序不需要)

I have some services that an application needs running in order for some of the app's features to work. I would like to enable the option to only start the external Windows services to initialize after the application is launched. (as opposed to having them start automatically with the machine and take up memory when the application is not needed)

我没有访问exe文件的code来实现这一点,所以我非常希望写一个C#.NET Windows服务时,推出一个exe时,将监控。

I do not have access to the exe's code to implement this, so ideally I would like to write a C# .Net Windows service that would monitor when an exe is launched.

我发现到目前为止是System.IO.FileSystemEventHandler。此组件只处理更改,创建,删除,并重新命名为事件类型。我不认为一个文件系统组件将是理想的地方找到我要找的,但不知道还有什么地方去了。

What I've found so far is the System.IO.FileSystemEventHandler. This component only handles changed, created, deleted, and renamed event types. I don't expect that a file system component would be the ideal place to find what I'm looking for, but don't know where else to go.

也许我不是寻找与正确的关键字,但我还没有找到任何关于谷歌或在这里非常有帮助的stackoverflow.com。

Maybe I'm not searching with the right keywords, but I have not yet found anything extremely helpful on Google or here on stackoverflow.com.

该解决方案将需要在XP,Vista和Win 7的运行,当谈到...

The solution would be required to run on XP, Vista, and Win 7 when it comes...

先谢谢您的任何指针。

推荐答案

从this文章,您可以使用WMI(即 System.Management 命名空间)的服务来监视进程启动事件。

From this article, you can use WMI (the System.Management namespace) in your service to watch for process start events.

 void WaitForProcess()
{
    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();
}

  static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e) {
    stopWatch.Stop();
    Console.WriteLine("Process stopped: {0}"
                      , e.NewEvent.Properties["ProcessName"].Value);
  }

  static void startWatch_EventArrived(object sender, EventArrivedEventArgs e) {
    startWatch.Stop();
    Console.WriteLine("Process started: {0}"
                      , e.NewEvent.Properties["ProcessName"].Value);
  }
}

WMI允许相当复杂的查询;您可以修改查询,这里触发事件处理程序,只有当你看到应用程序启动,或其他标准。 下面是一个简单的介绍,从C#的观点。