C # Start and Stop a Windows Service Group

Source: Internet
Author: User
Tags management studio

Windows Services are familiar to everyone. The concept of Windows Service Group does not seem to have such a statement in MS.

As a software developer, our machines are equipped with various development tools along with various related services.

Visual Studio can be disabled, and SqlServer Management Studio can be disabled, but SqlServer service is enabled by default. After work, my computer wanted to be used for life and entertainment without the need for database services. Especially after installing the Oracle database, I felt that the machine was very hard.

It is very troublesome to disable the service in sequence after each boot, or set the manual enable mode to enable the Service in sequence during each operation. Therefore, I want to package these services, package them into a service group, and start and stop services through programs.

In this way, you can set SqlServer, Oracle, Vmware, and other services to be manually enabled, and select to enable them as needed.

The above nonsense is the background of tool writing and is also an Application Scenario Description. The code is attached below.

The definition of the service group. I used the INI configuration file. One configuration section is a server group. The Key and Value in the configuration section are the service description and service name.

The order in which the service is enabled depends on the configuration content. Therefore, if Oracle-like services have requirements on the order in which the service is enabled, you must define the order in the Service Group.

The Value is the service name, and the service name is not services. the value of the Name field viewed by msc. Right-click the service and you can see that the displayed name is actually the service display name. Here, the service name is required.

Shows the configuration file.

Note: INI file format:

[Section1]

Key1 = value1

Key2 = value2

Start the program, load the main form, and obtain the configuration section, that is, the Service Group.

1 string path = Directory.GetCurrentDirectory() + "/config.ini";2 List<string> serviceGroups = INIHelper.GetAllSectionNames(path);3 cboServiceGroup.DataSource = serviceGroups;

The INI service class, reference link: http://www.cnblogs.com/mahongbiao/p/3751153.html

To start and stop a service, you must introduce the System. ServiceProcess assembly.

Start the Service Group:

1 if (string. isNullOrEmpty (cboServiceGroup. text) 2 {3 MessageBox. show ("select the Service Group to operate"); 4 return; 5} 6 // 7 string path = Directory. getCurrentDirectory () + "/config. ini "; 8 string section = cboServiceGroup. text; 9 string [] keys; 10 string [] values; 11 INIHelper. getAllKeyValues (section, out keys, out values, path); 12 // 13 foreach (string value in values) 14 {15 ServiceController SC = new ServiceControl Ler (value); 16 // 17 try18 {19 ServiceControllerStatus scs = SC. Status; 20 if (scs! = ServiceControllerStatus. running) 21 {22 try23 {24 SC. start (); 25} 26 catch (Exception ex) 27 {28 MessageBox. show ("service startup failed \ n" + ex. toString (); 29} 30} 31} 32 catch (Exception ex) 33 {34 MessageBox. show ("no service" + value); 35} 36 // 37} 38 // 39 MessageBox. show ("Service Startup completed ");

Stop Service Group

1 if (string. isNullOrEmpty (cboServiceGroup. text) 2 {3 MessageBox. show ("select the Service Group to operate"); 4 return; 5} 6 // 7 string path = Directory. getCurrentDirectory () + "/config. ini "; 8 string section = cboServiceGroup. text; 9 string [] keys; 10 string [] values; 11 INIHelper. getAllKeyValues (section, out keys, out values, path); 12 // 13 foreach (string value in values) 14 {15 ServiceController SC = new ServiceControl Ler (value); 16 try17 {18 ServiceControllerStatus scs = SC. Status; 19 if (scs! = ServiceControllerStatus. stopped) 20 {21 try22 {23 SC. stop (); 24} 25 catch (Exception ex) 26 {27 MessageBox. show ("service stop failed \ n" + ex. toString (); 28} 29} 30} 31 catch (Exception ex) 32 {33 MessageBox. show ("no service" + value); 34} 35 // 36 37} 38 // 39 MessageBox. show ("Service stopped"); 40}

 

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.