How on C # to check whether the Windows service "X" is running or not, and find out the type of startup (auto) and change the settings-off, turn on, put in autorun?

  • I can indicate the API list ... - Vladimir Martyanov
  • @ "Vladimir Martyanov" Well, write an example - Rakzin Roman

2 answers 2

There is a class ServiceController . I think that its functionality will be enough and you do not need to go to WinApi.

UPD:

The only thing that is missing in the ServiceController, as I understand it, is the startup type change.

In this case, you can use the methods from here .

Offered:

  1. Turn to WinApi

  2. Turn to WMI

  3. Refer to CMD

Another option found through the registry:

using System; using Microsoft.Win32; public enum ServiceStart { Boot = 0, System = 1, Automatic = 2, Manual = 3, Disabled = 4 } public class ServiceController2 : System.ServiceProcess.ServiceController { public ServiceStart ServiceStart { get { RegistryKey key = Registry.LocalMachine.OpenSubKey( "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName ); ServiceStart start = (ServiceStart)key.GetValue("Start"); key.Close(); return (start); } set { RegistryKey key = Registry.LocalMachine.OpenSubKey( "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName, true ); key.SetValue( "Start", (int)value ); key.Close(); } } } } 
  • WMI in PowerShell is good, in C # not very good - Alexander Alexeev

WinAPI list:

OpenSCManager() + OpenService() - opens a specific service by name. At the same time checking for the availability of such a service in principle. Then:
QueryServiceStatusEx() - current service state
QueryServiceConfig() - startup type
ChangeServiceConfig() - change parameters, including the type of launch
ControlService() - suspend or stop the service.