Windows Services)

Source: Internet
Author: User
Tags mssqlserver

You can use the system. serviceprocess. servicecontroller class to control Windows Services.
The Cui project must be referenced by system. serviceprocess.

Note that the service name and display name are different. We use the service name when calling. Right-click the service name, and the details are displayed in the Properties window.
For automatic service update, "Automatic Updates" is the display name, and its service name is: wuauserv.

1. Obtain all services and their statuses

Servicecontroller [] SCS = servicecontroller. getservices ();
Foreach (servicecontroller SC in SCS)
{
Console. writeline ("{0} ({1}); Status: {2}", SC. displayname, SC. servicename, SC. status );
}

2. Check whether the service has been installed

Bool exists = false;
Servicecontroller [] SCS = servicecontroller. getservices ();
Foreach (servicecontroller SC in SCS)
{
If (string. Compare (SC. servicename, "MSSQLServer", true) = 0)
{
Exists = true;
Break;
}
}

3. Check service status

Servicecontroller SC = new servicecontroller ("MSSQLServer ");
If (SC. Status = servicecontrollerstatus. Stopped)
Console. writeline ("STOPPED ");

4. start or stop the service (canstop and other attributes may also be used)

Servicecontroller SC = new servicecontroller ("MSSQLServer ");
If (SC. Status = servicecontrollerstatus. Stopped)
{
SC. Start ();
SC. waitforstatus (servicecontrollerstatus. Running );
}
Else
{
SC. Stop ();
SC. waitforstatus (servicecontrollerstatus. Stopped

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.