11

How can I get the path to the executable of a specific windows service from another program ? Unfortunately the class ServiceController (System.ServiceProcess) doesn't provide a method or property for that !

Kottan
  • 4,944
  • 9
  • 41
  • 67
  • What are you trying to achieve? – eemz Jun 18 '10 at 16:04
  • possible duplicate of [How to get service executable file path.](http://stackoverflow.com/questions/1045065/how-to-get-service-executable-file-path) – marc_s Jun 18 '10 at 16:12
  • 1
    @marc_s: I'm not sure it's an exact duplicate, the other one seems to ask specifically for a non admin way of doing it, which isn't mentioned in this one. – Hans Olsson Jun 18 '10 at 16:15

2 Answers2

18

There's always the WMI class Win32_Service as described here, specifically the PathName.

This works:

ManagementClass mc = new ManagementClass("Win32_Service");
foreach(ManagementObject mo in mc.GetInstances())
{
    if(mo.GetPropertyValue("Name").ToString() == "<Short name of your service>")
    {
        return mo.GetPropertyValue("PathName").ToString().Trim('"');
    }
}
Richard Dingwall
  • 2,692
  • 1
  • 31
  • 32
Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 3
    The PathName seems like the actual commandline for the service, i.e. it includes the parameters along with the executable filepath. – musaul Jan 06 '12 at 16:26
  • 1
    And do not forger to add `System.Management` in your project reference. Source : http://stackoverflow.com/a/1798212/1529139 – 56ka Jan 24 '14 at 14:23
6

You can obtain them from here using the Registry in HKLM:

 System\CurrentControlSet\Services\Service

Look for the ImagePath value.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636