vs2015 Developing Windows Services

Source: Internet
Author: User

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

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.