Is it possible to stop the service cycle? I have a list of services, put them in the List
List<string> name = new List<string> { "AdobeARMservice", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV", "Telnet", "mnmsrvc", "Schedule", "seclogon", "SessionEnv", "bthserv", "WinRM", "SCardSvr", "WbioSrvc", "SCPolicySvc","RtkBleServ"}; Somehow I tried:
foreach (ServiceController service in ServiceController.GetServices()) { if (service.ServiceName == name[15]) { service.Stop(); Console.WriteLine("Остановлен"); } } Or how can it be different?
Solved the question:
on Linq!
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.ServiceProcess; namespace ConsoleApplication25 { class Program { static List<string> services = new List<string> { "AdobeARMService", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV" }; static void Main(string[] args) { Task.Factory.StartNew(() => Run()); Console.ReadLine(); } static void Run() { while (true) { var temp = ServiceController.GetServices().Where(s => services.Contains(s.ServiceName)).ToList(); temp.ForEach(t => { try { t.Stop(); } catch { } }); Thread.Sleep(500); } } } } without Linq
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.ServiceProcess; namespace ConsoleApplication25 { class Program { static List<string> services = new List<string> { "AdobeARMService", "RemoteRegistry", "TermService", "Messenger", "SSDPSRV" }; static void Main(string[] args) { new Thread(Run) { IsBackground = true }.Start(); Console.ReadLine(); } static void Run() { while (true) { List<ServiceController> temp = new List<ServiceController>(); foreach (var s in ServiceController.GetServices()) { if (services.Contains(s.ServiceName)) { temp.Add(s); } } foreach (var s in temp) { try { s.Stop(); } catch { } } Thread.Sleep(500); } } } }