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
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:
Turn to WinApi
Turn to WMI
- 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 stateQueryServiceConfig() - startup typeChangeServiceConfig() - change parameters, including the type of launchControlService() - suspend or stop the service.