如何从ServiceController的确定Windows.Diagnostics.Process [英] How to determine Windows.Diagnostics.Process from ServiceController

查看:135
本文介绍了如何从ServiceController的确定Windows.Diagnostics.Process的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一篇文章,所以让我说HELLO开始!

This is my first post, so let me start by saying HELLO!

我写一个Windows服务来监视在同一台服务器上的其他一些窗口服务的运行状态。我想扩展应用程序同时打印一些服务的内存统计数据,但我有麻烦工作如何从一个特定的ServiceController对象映射到其关联Diagnostics.Process对象,我想我需要确定内存状态。

I am writing a windows service to monitor the running state of a number of other windows services on the same server. I'd like to extend the application to also print some of the memory statistics of the services, but I'm having trouble working out how to map from a particular ServiceController object to its associated Diagnostics.Process object, which I think I need to determine the memory state.

我发现了如何从一个ServiceController的原始图像名称映射,但有若干服务我监控从相同图象开始,所以这将不足以确定过程。

I found out how to map from a ServiceController to the original image name, but a number of the services I am monitoring are started from the same image, so this won't be enough to determine the Process.

有谁知道如何从给定的ServiceController得到一个Process对象?或许通过确定一个服务的PID?否则没有任何人有另一种解决这一问题?

Does anyone know how to get a Process object from a given ServiceController? Perhaps by determining the PID of a service? Or else does anyone have another workaround for this problem?

非常感谢,亚历克斯

推荐答案

System.Management应该在这种情况下,为你工作。下面是一个示例,让你开始:

System.Management should work for you in this case. Here's a sample to get you started:

using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Management;
class Program
{
    static void Main(string[] args)
    {
        foreach (ServiceController scTemp in ServiceController.GetServices())
        {
            if (scTemp.Status == ServiceControllerStatus.Stopped)
                continue;    // stopped, so no process ID!

            ManagementObject service = new ManagementObject(@"Win32_service.Name='" + scTemp.ServiceName + "'");
            object o = service.GetPropertyValue("ProcessId");
            int processId = (int) ((UInt32) o);
            Process process = Process.GetProcessById(processId);
            Console.WriteLine("Service: {0}, Process ID: {1}", scTemp.ServiceName, processId);
        }
    }
}

这篇关于如何从ServiceController的确定Windows.Diagnostics.Process的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆