Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
Ofir Avatar asked Jan 14 '23 15:01

Ofir


1 Answers

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();
}
like image 141
Chris Clayton Avatar answered Jan 24 '23 22:01

Chris Clayton