This article describes how to create, install, start, monitor, and uninstall simple Windows Service content steps and Considerations in C #, and the friends you need can refer to the following
One, create a Windows Service
1) Create a Windows service project
2) Renaming the service
Rename Service1 to your service name, where we are named Servicetest.
Second, create the service installation program
1) Add Installer
We can then see that the ProjectInstaller.cs and 2 installed components are created automatically for us.
2) Modify the installation service name
Right-ServiceInsraller1, select Properties, and change the value of ServiceName to servicetest.
3) Modify Installation permissions
Right-ServiceProcessInsraller1, select Properties, and change the account value to LocalSystem.
Third, write the service code
1) Open Servicetest Code
Right-click servicetest and select 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, start the service to write a log, close the time to write a log.
Iv. Creating the installation script
Add 2 files to 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 Servicetest
sc config servicetest start= auto
2) Uninstall Script Uninstall.bat
%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe/u WindowsServiceTest.exe
3) Installation Script description
The second behavior starts the service.
The third behavior sets the service to run automatically.
These 2 lines are selected on a service basis.
4) Script debugging
If you need to see the script health, add pause to the last line of the script
V. Control of services in C #
0) Configuring the directory structure
CV a new WPF project, called Windowsservicetestui, adds a reference to the system.serviceprocess.
Create the service directory under the Windowsservicetestui bin\debug directory.
Set the build directory for windowsservicetest to the service directory created above.
Post-build directory structure such as
1) Installation
The installation will cause directory problems, so the installation code is as follows:
string currentdirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = currentdirectory + "\\Service"; Process process = new process ();p rocess. Startinfo.useshellexecute = false;process. Startinfo.filename = "Install.bat";p rocess. Startinfo.createnowindow = true;process. Start (); System.Environment.CurrentDirectory = CurrentDirectory;
2) Uninstall
A directory problem also occurs when uninstalling, so the uninstall code is as follows:
string currentdirectory = System.Environment.CurrentDirectory; System.Environment.CurrentDirectory = currentdirectory + "\\Service"; Process process = new process ();p rocess. Startinfo.useshellexecute = false;process. Startinfo.filename = "Uninstall.bat";p rocess. Startinfo.createnowindow = true;process. Start (); System.Environment.CurrentDirectory = CurrentDirectory;
3) Start
The code is as follows:
Using System.ServiceProcess; ServiceController ServiceController = new ServiceController ("Servicetest"); Servicecontroller.start ();
4) Stop
ServiceController ServiceController = new ServiceController ("Servicetest"); if (servicecontroller.canstop) Servicecontroller.stop ();
5) Pause/Resume
ServiceController ServiceController = new ServiceController ("Servicetest"); Servicecontroller.canpauseandcontinue) {if (Servicecontroller.status = = servicecontrollerstatus.running) Servicecontroller.pause (); else if (servicecontroller.status = = servicecontrollerstatus.paused) Servicecontroller.continue ();}
6) Check the status
ServiceController ServiceController = new ServiceController ("Servicetest"); string Status = ServiceController.Status.ToString ();
Vi. Debugging Windows Service
1) Install and run the service
2) Additional Process
3) Add breakpoints in your code to debug
Vii. Summary
The above configuration for Windows service is not explained in detail in this article, but as described above, you can create a running Windows service that meets the needs of your work.