C # Start and stop for Windows service groups

Source: Internet
Author: User
Tags sql server management sql server management studio

Windows Services Everyone is not unfamiliar with the concept of Windows Service Group, seemingly MS does not have this argument.

As a software developer, our machines are equipped with a variety of development tools, accompanied by a variety of related services.

Visual Studio can not open, SQL Server Management Studio can not be opened, but SQL Server service is turned on by default. After work, my computer would like to use for life, entertainment, and do not need database services these things, especially after the installation of the Oracle database, I feel the machine is struggling.

After each power-on to turn off the service, or set up manual mode, each time the work is used to turn on the service, is a very troublesome thing. Therefore, I say these related services are packaged, packaged as a service group concept, and through the program to achieve service start and stop.

This allows me to set up the services for SQL Server, Oracle, VMware, etc. to be manually turned on and then select Open when needed.

The above nonsense for the tool writing background, is also a description of the application scenario, the below attached code.

Service group definition, I used the INI configuration file, a configuration section for a server group, the configuration section of key, value for the service description and service name.

The order in which the service is opened is determined by the configuration content, so it is necessary to define the order of the service groups like Oracle.

The value is the service name, the service name is not the value of the Services.msc View Name field, right-click the service, you can see that the display name is actually the display name of the service, where the service name is required.

The configuration file looks like this

Note: INI file format:

[Section1]

Key1=value1

Key2=value2

The program starts, the main form loads, and gets the configuration section, which is the service group.

1 string " /config.ini " ; 2 list<string> servicegroups = inihelper.getallsectionnames (path); 3 cboservicegroup.datasource = servicegroups;

Among the INI service classes, refer to link: http://www.cnblogs.com/mahongbiao/p/3751153.html

The start and stop of the service requires the introduction of the System.ServiceProcess assembly.

To start a service group:

1 if(string. IsNullOrEmpty (cboservicegroup.text))2 {3MessageBox.Show ("Please select a service group to operate on");4     return;5 }6 //7 stringPath = directory.getcurrentdirectory () +"/config.ini";8 stringSection =Cboservicegroup.text;9 string[] keys;Ten string[] values; OneInihelper.getallkeyvalues (section, outKeys outvalues, path); A // - foreach(stringValueinchvalues) - { theServiceController sc =NewServiceController (value); -     // -     Try -     { +ServiceControllerStatus SCS =SC. Status; -         if(SCS! =servicecontrollerstatus.running) +         { A             Try at             { - SC. Start (); -             } -             Catch(Exception ex) -             { -MessageBox.Show ("Service failed to start \ n"+Ex. ToString ()); in             } -         } to     } +     Catch(Exception ex) -     { theMessageBox.Show ("Service not present"+value); *     } $     //  Panax Notoginseng } - // theMessageBox.Show ("Service Start Complete");

Stop a service group

1 if(string. IsNullOrEmpty (cboservicegroup.text))2 {3MessageBox.Show ("Please select a service group to operate on");4     return;5 }6 //7 stringPath = directory.getcurrentdirectory () +"/config.ini";8 stringSection =Cboservicegroup.text;9 string[] keys;Ten string[] values; OneInihelper.getallkeyvalues (section, outKeys outvalues, path); A // - foreach(stringValueinchvalues) - { theServiceController sc =NewServiceController (value); -     Try -     { -ServiceControllerStatus SCS =SC. Status; +         if(SCS! =servicecontrollerstatus.stopped) -         { +             Try A             { at SC. Stop (); -             } -             Catch(Exception ex) -             { -MessageBox.Show ("service stopped failing \ n"+Ex. ToString ()); -             } in         } -     } to     Catch(Exception ex) +     { -MessageBox.Show ("Service not present"+value); the     } *     // $ Panax Notoginseng } - // theMessageBox.Show ("Service stops completing"); +}

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.