Create a Windows (NT) service in C #

Source: Internet
Author: User
Tags command line
Window| creates a Windows service that is called NT services in previous versions of Visual Studio, and new names are enabled in Vs.net. Creating a Windows service in Visual C # is not a difficult thing, and this article will guide you through creating a Windows service and using it step-by-step. This service writes some textual information to a text file when it is started and stopped. This tip comes from MSDN, but now the program is written more clearly and you can modify it to suit your needs. Downloadable Project package: Mcwinservice.zip.
First step: Create a service framework
To create a new Windows service, you can choose the Windows Services (Windows Service) option from the Visual C # project, give the project a new file name, and then click OK. The result seems to be this:


As you can see, add the WebService1.cs class to the project file:


Set the ServiceName (service name) to the name you want, so it's easier to identify your service during the test, or you can write code to set this property, by using this. ServiceName = "Mcwinservice":


The default code that the wizard adds to the WebService1.cs.
Step Two: Add functionality to the service
In WebService1.cs we can see that there are two neglected functions OnStart and OnStop.

The OnStart function executes when the service is started, and the OnStop function executes when the service is stopped. Here, when the service is started and stopped, write some text information to a text file, which is the code.
Step three: Install and run the service
This application finally generates an EXE file McWinService.exe. To register this service, you need to start the command line in the Start menu to invoke Installutil:installutil C:\mcWebService\bin\Debug\mcWebService.exe as follows. To uninstall the service, use the/u option: installutil/u C:\mcWebService\bin\Debug\mcWebService.exe.

Step Fourth: Start and stop the service
Right-click on the desktop icon "My Computer" to select the Admin menu item (Manage), and then start the Services (service) in service and application (services and applications); You can also select Computer Management from the Admin tool from the Start menu. (Computer Management) into service. So you can see the service Mcwinservice, right click on the pop-up menu has the start and stop option.


Step Fifth: Testing services
Go to the temp directory to see if the text file exists or write to the content.



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.