Post

Windows Service File Name

This post talks about how to get Windows Service executable name and path.

When you get a ServiceController instance, how do you know the executable name and path? WMI makes it possible. You can use this GetServicePath function to query service executable name and path.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static string GetServicePath(ServiceController service)
{
    //construct the management path
    string path = "Win32_Service.Name='" + service.ServiceName + "'";
    ManagementPath p = new ManagementPath(path);
    //construct the management object
    ManagementObject ManagementObj = new ManagementObject(p);
    if (ManagementObj["pathName"] != null)
    {
        return ManagementObj["pathName"].ToString().Replace("\"", null);
    }
    else
    {
        return null;
    }
}
© Lex Li. All rights reserved. The code included is licensed under CC BY 4.0 unless otherwise noted.
Advertisement