. NET create a Windows service get started

Source: Internet
Author: User

This article mainly records learning. NET How to create a Windows service.

1. Create a Windows service program

2. Create a New installation program

3. Modify the service file

The code is as follows

  protected Override voidOnStart (string[] args) {            using(System.IO.StreamWriter SW =NewSystem.IO.StreamWriter ("C:\\log.txt",true) ) {SW. WriteLine (DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +"Start."); }        }        protected Override voidOnStop () {using(System.IO.StreamWriter SW =NewSystem.IO.StreamWriter ("C:\\log.txt",true) ) {SW. WriteLine (DateTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") +"Stop."); }        }

4. Using Installutil.exe to install the service

To open cmd with a tube machine operator

Execute command in switch to Bin directory

%systemroot%\microsoft.net\framework\v4. 0.30319\installutil.exe WindowsService2.exe

Note: Here to adjust the permissions of the installer:

5. Start the service

At this point in the C drive to generate a Log.txt file content as follows:

Stop the service and add the following in the Log.txt file

Add:

For more convenient installation and uninstallation of the service, two scripts were written. (both need to be performed with administrator privileges)

(1) Installation script

Set projectroot=D:\document\work\demo\windowServiceDemo\WindowsService2\bin\Debugcmd/k%systemroot%\ Microsoft.net\framework\v4. 0.30319\installutil.exe%projectroot%/windowsservice2.exe

(2) Uninstall script

Set projectroot=D:\document\work\demo\windowServiceDemo\WindowsService2\bin\Debugcmd/k%systemroot%\ Microsoft.net\framework\v4. 0.30319\installutil.exe/u%projectroot%/windowsservice2.exe

. NET create a Windows service get started

Related Article

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.