从services.msc c#获取服务路径

Ofi*_*fir 3 c# service servicecontroller

我正在尝试从services.msc获取服务可执行文件路径

我写了下一个代码:

  var service = ServiceController.GetServices().Where(p => p.ServiceName.Equals("Service name", StringComparison.InvariantCultureIgnoreCase));
if (service.Any())
//get service data
Run Code Online (Sandbox Code Playgroud)

我找不到服务可执行路径所在的位置(如果有的话)?

在services.msc中,我可以看到路径,所以我假设它也可以通过代码获取它. services.msc中的服务信息示例(请参阅那里存在可执行路径)

有任何想法吗?

Chr*_*ton 7

您可以从注册表中获取它,如下所示:

private static string GetServiceInstallPath(string serviceName)
{
    RegistryKey regkey;
    regkey = Registry.LocalMachine.OpenSubKey(string.Format(@"SYSTEM\CurrentControlSet\services\{0}", serviceName));

    if (regkey.GetValue("ImagePath") == null)
        return "Not Found";
    else
        return regkey.GetValue("ImagePath").ToString();
}
Run Code Online (Sandbox Code Playgroud)