Create a Windows (NT) service with C #

zhaozj2021-02-11  225

Create a Windows (NT) service with C #

Green Apple Studio April 27, 2001 17: 58Windows Service is called NT service in Visual Studio, and new names are enabled in VS.NET. Creating a Windows service with Visual C # is not a difficult thing, this article will guide you step by step to create a Windows service and use it. This service writes some text messages to a text file when starting and stopping. This tip is from MSDN, but now the program is more clear, and you can modify it according to your needs. Download Engineering File: McWinService.zip. Step 1: Creating a Service Frame To create a new Windows service, you can select the Windows Service option from the Visual C # project to give the project a new file name, then click OK. The result looks like this: You can see that adding the webservice1.cs class to the guide engineering document: Set the ServiceName to your own name, so it is easier to identify your service during the test. This property can be set by writing code, the method is this.ServiceName = "McWInService": The wizard adds the default code in WebService1.cs. Step 2: Add functions to the service to WebService1.cs we can see that there are two neglected functions onstart and onstop. The onStart function is executed when the service is started, and the onStop function is executed when the service is stopped. Here, when starting and stopping the service, write some text messages in a text file, the code. Step 3: Installing and running 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 call 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 4: Starting and stopping the service in the desktop with right-click the icon "My Computer" Select Management Menu Item (SERVICE), then "Services and Applications" in the service (SERVICES); you can also Select Computer Management in the Management Tool directly from the Start menu. This way you can see the service McWInService, right-click the playback and stop option in the pop-up menu. Step 5: Test services Go to the TEMP directory to see if the text file exists, there is no write content.

转载请注明原文地址:https://www.9cbs.com/read-4560.html

New Post(0)