Path to the executable of a windows service

丶灬走出姿态 提交于 2019-12-20 10:38:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!