Post

ServiceController Extensions

In the afternoon I had to do something with Windows services. As a result a better ServiceController should be used to know the executable name.

I searched on CodeProject.com. Yes, there is an old article. In the comments section, the technique was mentioned that System.Management should be used.

http://www.codeproject.com/cs/system/extendservicecontroller.asp?print=true

Fine, everything works well as expected. Thanks a lot, Mohamed Sharaf.

BTW, it is still not easy to use the code in the sample because I need to call ServiceController.GetServices() so I write a static function instead,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public 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();
    }
    else
    {
        return null;
    }
}
© Lex Li. All rights reserved. The code included is licensed under CC BY 4.0 unless otherwise noted.