* This Article is not original to the blogger. The original author's article link is not saved in the favorites folder. Sorry, the copyright belongs to the original author. This document is only slightly modified.
1. Create a new C # Windows service: windows service Project
OnStart (
3. After the C # Windows service is created, set the service running cycle. There are two timmer in the ToolBox on the left, one under the component and the other under the windows form, unfortunately, neither of them can be used. We need to manually create a timmer and set its attributes and events.
= System.Timers.Timer(); t.Elapsed += System.Timers.ElapsedEventHandler(TimeElapse); t.AutoReset = ; t.Enabled = ;= FileStream(= + DateTime.Now.ToString() +
4. After the service is compiled, the SCM (Service Control Manager) cannot manage the service. You need to add a loader to the service. In the design view of Service1.cs, right-click and select "add loader". By default, the project Installer. cs class is added.
5. After adding the class, you can see serviceInstaller1 and serviceProcessInstaller1 in the design view of the class and set their attributes respectively.
Set the running mode of serviceInstaller1 to manual or automatic
Set the ServiceName of serviceInstaller1, set why, what is displayed in the service list
Set the Running Account of serviceProcessInstaller1 to LocalSystem
6. compile the project
7. Use the command line tool provided by vs to run the exe compiled by installutil.
8. You can see the service we created in the system service.
Note that:
And then reinstall the service. In addition, the C # Windows Service has no interface. Do not output some information in the controlled output mode. You can only add one EventLog and write logs through WriteEntry.