In C #, you use the ServiceController class to control the Windows service, using the reference: System.ServiceProcess before you use it, and then reference in the namespace: using System.ServiceProcess. Here's an example of getting all of the installed Windows services and apps for this computer, and then finding out if an app live service is already installed.
Code:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.serviceprocess;namespace Determine if a service or application is installed in the machine {public partial class Form1:form {public Form1 () {Initializecomponen T (); } servicecontroller[] Services = servicecontroller.getservices (); private bool Existsth () {bool exist = false; for (int i = 0; i < services.length; i++) {if (Services[i]. displayname.tostring () = = TextBox1.Text.Trim ()) exist = true; } return exist; private void Button1_Click (object sender, EventArgs e) {if (Existsth ()) Message Box.show ("Installed"); Else MessageBox.Show ("not Installed"); private void Form1_Load (object sender, EventArgs e) { for (int i = 0; i < services.length; i++) ListBox1.Items.Add (Services[i]. Displayname.tostring ()); } }}
Assuming that a service name is servicesname, write the start service, stop the service, and restart the service code as follows:
Private ServiceController _controller;private void StopService () { This._controller = new ServiceController (" Servicesname "); This._controller. Stop (); This._controller. WaitForStatus (servicecontrollerstatus.stopped); This._controller. Close ();} private void StartService () { This._controller = new ServiceController ("Servicesname"); This._controller. Start (); This._controller. WaitForStatus (servicecontrollerstatus.running); This._controller. Close ();} private void Resetservice () { This._controller = new ServiceController ("Servicesname"); This._controller. Stop (); This._controller. WaitForStatus (servicecontrollerstatus.stopped); This._controller. Start (); This._controller. WaitForStatus (servicecontrollerstatus.running); This._controller. Close ();}
C # uses ServiceController to control Windows services