Teach you how to create a Windows service through C #

Source: Internet
Author: User

This article reprinted: http://www.cnblogs.com/xiurui12345/archive/2012/05/16/2503868.html

Windows service is not complicated, but there are too many things to be paid attention to, and the information on the Internet is also messy. Occasionally, writing on your own will be lost. Therefore, this article will be generated. This article will not write complicated things and will be completely written based on the needs of basic applications, so it will not be very in-depth on Windows service.

This section describes how to use C # To create, install, start, monitor, and uninstall a simple Windows service.

1. Create a Windows service1) create a Windows Service Project

2) Rename the service

Rename service1 as your service name. Here we name it servicetest.

2. Create a service for Installation Program 1) Add the installer

Then we can see that projectinstaller. CS and two installed components are automatically created for us.

2) modify the name of the installation service

Right-click serviceinsraller1, select properties, and change the value of servicename to servicetest.

3) modify the installation permission

Right-click serviceprocessinsraller1, select properties, and change the account value to LocalSystem.

Iii. Write Service Code 1) Open servicetest code

Right-click servicetest and choose view code.

2) Write Service Logic

Add the following code:

?
1234567891011121314151617181920212223242526272829303132333435 Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. diagnostics; Using System. LINQ; Using System. serviceprocess; Using System. text; Namespace Windowsservicetest { Public Partial Class Servicetest: servicebase { Public Servicetest () { Initializecomponent (); } Protected Override Void Onstart ( String [] ARGs) { Using (System. Io. streamwriter Sw = New System. Io. streamwriter ( "C: \ log.txt" , True )) { Sw. writeline (datetime. Now. tostring ( "Yyyy-mm-dd hh: mm: SS" ) + "Start ." ); } } Protected Override Void Onstop () { Using (System. Io. streamwriter Sw = New System. Io. streamwriter ( "C: \ log.txt" , True )) { Sw. writeline (datetime. Now. tostring ( "Yyyy-mm-dd hh: mm: SS" ) + "Stop ." ); } } } }

Here, our logic is very simple. Write a log when starting the service, and then write a log when it is disabled.

4. Create an installation script

Add two files to the project as follows (must beANSIOrUTF-8 without BOM format):

1) installation script install. bat?
123 % SystemRoot % \ Microsoft. NET \ framework \ v4.0.30319 \ installutil.exe windowsservicetest.exeNet start servicetestSC config servicetest start = auto
2) uninstall the script Uninstall. bat?
1 % SystemRoot % \ Microsoft. NET \ framework \ v4.0.30319 \ installutil.exe/U Windowsservicetest.exe
3) installation script description

Second, start the service.

Third, set the service to run automatically.

The two rows are selected based on the service form.

4) script debugging

To view the script running status, addPause

5. Control the service in C #0) configure the directory structure

Resume A new WPF project called windowsservicetestui.System. serviceprocess.

Create a service directory under the bin \ DEBUG directory of windowsservicetestui.

Set the generated directory of windowsservicetest to the service directory created above.

The directory structure after generation is shown in figure

 

1) Installation

The installation code is as follows:

?
12345678 String Currentdirectory = system. environment. currentdirectory; System. environment. currentdirectory = currentdirectory + "\ Service" ; Process = New Process (); Process. startinfo. useshellexecute = False ; Process. startinfo. filename = "Install. Bat" ; Process. startinfo. createnowindow = True ; Process. Start (); System. environment. currentdirectory = currentdirectory;
2) uninstall

Directory issues also occur during uninstallation, so the uninstallation code is as follows:

?
12345678 String Currentdirectory = system. environment. currentdirectory; System. environment. currentdirectory = currentdirectory + "\ Service" ; Process = New Process (); Process. startinfo. useshellexecute = False ; Process. startinfo. filename = "Uninstall. Bat" ; Process. startinfo. createnowindow = True ; Process. Start (); System. environment. currentdirectory = currentdirectory;
3) Start

The Code is as follows:

?
12345 Using System. serviceprocess;Servicecontroller =New Servicecontroller ("Servicetest");Servicecontroller. Start ();
4) Stop?
123 Servicecontroller =New Servicecontroller ("Servicetest");If (Servicecontroller. canstop)Servicecontroller. Stop ();
5) pause/continue?
12345678 Servicecontroller = New Servicecontroller ( "Servicetest" ); If (Servicecontroller. canpauseandcontinue) { If (Servicecontroller. Status = servicecontrollerstatus. Running) Servicecontroller. Pause (); Else If (Servicecontroller. Status = servicecontrollerstatus. paused) Servicecontroller. Continue (); }
6) Check the status?
12 Servicecontroller =New Servicecontroller ("Servicetest");String Status = servicecontroller. Status. tostring ();
6. debug windows service1) install and run the Service 2) add-on Process

3) Add a breakpoint to the Code for debugging.

VII. Summary

This article does not provide a detailed description of the above configurations of Windows service, but you can follow the steps above to create a Windows service that can run, so as to meet the work requirements.

See https://github.com/sorex/WindowsServiceTest for sample code

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.