Search code examples
c#serviceservicecontroller

Get service path from services.msc c#


I'm trying to get service executable path from services.msc

I wrote the next code:

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

I couldn`t find where (if at all) the service executable path is located?

In services.msc I can see the path so I'm assuming it also possible to get it through code. Example of service info in services.msc (see that executable path exists there)

Any Ideas?


Solution

  • You can get it from the registry like so:

    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();
    }