如何从 ServiceController 确定 Windows.Diagnostics.Process

发布于 2024-08-08 08:41:13 字数 372 浏览 2 评论 0原文

这是我的第一篇文章,所以让我首先说你好!

我正在编写一个 Windows 服务来监视同一服务器上许多其他 Windows 服务的运行状态。我想扩展应用程序以打印服务的一些内存统计信息,但我无法弄清楚如何从特定的 ServiceController 对象映射到其关联的 Diagnostics.Process 对象,我认为我需要确定内存状态。

我找到了如何从 ServiceController 映射到原始映像名称,但我正在监视的许多服务是从同一个映像启动的,因此这不足以确定进程。

有谁知道如何从给定的 ServiceController 获取 Process 对象?也许通过确定服务的 PID 来实现?或者还有人有其他解决方法来解决这个问题吗?

非常感谢,亚历克斯

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

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.

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.

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?

Many thanks, Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-08-15 08:41:13

在这种情况下,System.Management 应该适合您。这是一个帮助您入门的示例:

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

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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文