问题
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 !
回答1:
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('"');
}
}
回答2:
You can obtain them from here using the Registry in HKLM:
System\CurrentControlSet\Services\Service
Look for the ImagePath value.
来源:https://stackoverflow.com/questions/3071215/path-to-the-executable-of-a-windows-service