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 Service
1) 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 InstallationProgram
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 ServiceCode
1) Open servicetest code
Right-click servicetest and choose view code.
2) Write Service Logic
Add the following code:
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 2 files in the project as follows (must be ANSI or UTF-8 without BOM format ):
1) installation script install. bat
% SystemRoot % \ microsoft. Net \ framework \ v4.0.30319 \ installutil.exe windowsservicetest.exe Net start servicetestsc config servicetest start = auto
2) uninstall the script Uninstall. bat
% 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, add pause to the last line of the script.
5. Control Services in C #
0) configure the directory structure
Resume A new WPF project named windowsservicetestui, which adds a reference to 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:
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:
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:
Using system. serviceprocess; servicecontroller = new servicecontroller ("servicetest"); servicecontroller. Start ();
4) Stop
Servicecontroller = new servicecontroller ("servicetest"); If (servicecontroller. canstop) servicecontroller. Stop ();
5) pause/continue
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
Servicecontroller = new servicecontroller ("servicetest"); string status = servicecontroller. Status. tostring ();
6. debug Windows Service
1) install and run the service
2) Attach 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.
To create a new Windows service, you can select the Windows Service option from the Visual C # project, give the project a new file name, and then clickOK.
You can see that the wizard adds the webservice1.cs class to the project file:
The meaning of each attribute is:
ÜWhether autolog is automatically written to the System Log File
ÜReceive Power Events During canhandlepowerevent Service
ÜWhether the canpauseandcontinue Service accepts the pause or continue running request
ÜWhether the canshutdown Service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process
ÜWhether the canstop Service accepts the stop operation request
ÜServicename service name
3: To determine how to start the service, clickServiceinstallerComponent andStarttypeSet the property to an appropriate value.
ÜManualAfter the service is installed, you must start it manually.
ÜAutomaticThe service is automatically started every time the computer restarts.
ÜDisabledThe service cannot be started.
4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.
In this way, no matter which user is logged on to the system, the service will always start.