usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Diagnostics;usingSystem.Linq;usingsystem.serviceprocess;usingSystem.Text;usingSystem.IO;namespaceorganizclientsocketservice{ Public Partial classService1:servicebase { PublicService1 () {InitializeComponent (); System.Timers.Timer Timer=NewSystem.Timers.Timer (); Timer. Elapsed+=NewSystem.Timers.ElapsedEventHandler (timedevent); Timer. Interval= the;//executes once every 5 secondsTimer. Enabled =true; } //timed Execution Events Private voidTimedevent (Objectsender, System.Timers.ElapsedEventArgs e) { //Business Logic Code } protected Override voidOnStart (string[] args) { This. Writelog ("OnStart"); } protected Override voidOnStop () { This. Writelog ("OnStop"); } protected Override voidOnShutdown () { This. Writelog ("OnShutdown"); } #regionRecord log/// <summary> ///Record Log/// </summary> /// <param name= "msg" ></param> Private voidWritelog (stringmsg) { //string path = @ "C:\log.txt"; //The log file will exist under the Windows Service Program directory stringPath = AppDomain.CurrentDomain.BaseDirectory +"\\log.txt"; FileInfo file=NewFileInfo (path); if(!file. Exists) {FileStream fs; FS=file.create (path); Fs. Close (); } using(FileStream fs =NewFileStream (Path, Filemode.append, FileAccess.Write)) { using(StreamWriter SW =NewStreamWriter (FS)) {SW. WriteLine (DateTime.Now.ToString ()+" "+msg); } } } #endregion }}
5. Compile build, install Windows service to Windows system.
After the development is completed, the whole project is compiled and generated. In the Windows Service Development folder "\Bin\Debug", is the service we need to install, it is recommended to copy all the files inside the system to a directory to install.
I am copying the files in the whole folder to the C:\WindowService folder. Then open Directory C:\Windows\Microsoft.NET\Framework64\v4.0.30319, copy the InstallUtil.exe file inside to c \ Windowservice folder).
Note: My system is windows10,64 bit system, my service will also install to 64-bit system, so I was into C:\Windows\Microsoft.NET\Framework64\ v4.0.30319 directory copy InstallUtil.exe file. When you install, according to the target system you installed, to feel that it is a copy of which version of the framework, whether the specific 64-bit or 32-bit is also determined by your system.
Do the above work can be installed, open the CDM can be installed (be sure to run as an administrator yo, or the installation will be reported "Windows Service installation exception: System.Security.SecurityException: Source not found , but failed to search for some or all of the events ").
The following are the installation commands, the start Service command, the Stop Service command, the Uninstall Service command:
Install command:D:\WindowsService\Timer\InstallUtil.exe D:\WindowsService\Timer\WindowsServiceTest.exe
Start Service command: net start search Organiz client data Synchronization Service
Shutdown Service Command: net stop search Organiz client data Synchronization Service
Uninstall Service command:D:\WindowsService\Timer\InstallUtil.exe-u D:\WindowsService\Timer\WindowsServiceTest.exe
vs2015 Developing Windows Services