C # controls start and stop of "services" in the computer

Source: Internet
Author: User

The last time I published a "C # control" Ms-sqlserver service to start and stop, most of its content is collected online, today it refined. So the topic is as above.

In fact, all the services in the computer can be controlled by the computer, such as: IIS, Ms-sql Server and other services, we want to control its start and stop only need to find the service corresponding to the service name.

Steps: Right-click My Computer--> management-->--> Services and Applications--> in the Computer Management window Select Services--> Find the service you want to control right-click to select "Properties"--> on the General tab, "server name" One item (remember it, this is the name of the service you want)

Here's what you'll do in VS2005:

First, to add a reference system.serviceprocess

Second, you want to use namespaces in your program ServiceProcess

Code fragment:

Using System.ServiceProcess;

ServiceController serviceController1 = new ServiceController ();

Servicecontroller1.servicename = "server name";

Servicecontroller1.machinename = ".";

if (Servicecontroller1.status = = servicecontrollerstatus.running)

Servicecontroller1.stop ();

Else

Servicecontroller1.start ();

Related Article

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.