C # using ServiceController to control Windows services

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.