Create Windows Services (Windows Services) with C #

zhaozj2021-02-11  221

Create Windows Services (Windows Services) with C #

The Windows service is called NT service in the previous version of Visual Studio, and the new name is 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.

Step 1: Creating a Service Framework 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.

You can see that adding WebService1.cs to the guide engineering file:

Set the properties as shown below:

The integration of each attribute is:

ü AutoLog writes a log file for the system

ü CanhandLepowerevent service to receive power events

ü CanpauseAndContinue service is a request for suspend or continue running

ü CanshutDown service does notify it when it is turned off, in order to be able to call the onshutdown process

ü Canstop service accepts requests to stop running

ü Servicename service name

Step 2: Add functions to your service In the .cs code file we can see that there are two ignored 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 is as follows:

protected override void onstart (String [] ARGS)

{

FILESTREAM FS = New FileStream (@ "d: /mcwindowsservice.txt", filemode.openorcreate, fileaccess.write;

Streamwriter M_StreamWriter = New StreamWriter (FS);

M_StreamWriter.basestream.seek (0, seekorigin.end);

M_StreamWriter.writeline ("McWindowsService: Service Started" DateTime.now.toString () "/ n");

M_StreamWriter.flush ();

M_StreamWriter.Close ();

fs.close ();

}

protected override void onstop ()

{

FILESTREAM FS = New FileStream (@ "d: /mcwindowsservice.txt", filemode.openorcreate, fileaccess.write;

Streamwriter M_StreamWriter = New StreamWriter (FS);

M_StreamWriter.basestream.seek (0, seekorigin.end);

M_StreamWriter.writeline ("McWindowsservice: Service Stopped DateTime.now.toString () " / n "); m_streamwriter.flush ();

M_StreamWriter.Close ();

fs.close ();

}

Step 3: Add the installer to the service application

Visual Studio.net comes with a installation component that can be used to install resources associated with the service application. Installing components Register a single service on the system being installed and enables the Service Control Manager to know the service.

To properly install the service, do not need any special coding in the installer. However, if you need to add special features to the installation process, you may have to modify the contents of the installer.

The step of adding the installer to the service application is:

1: In the solution, access the Design view of the service you want to add to the installation component.

2: In the Properties window, click Add Setup Link

At this time, a new class ProjectInstaller and two installation components ServiceProcessInstaller and ServiceInstaller are added to the project, and the attribute value of the service is copied to the component.

3: To determine how to start the service, click the ServiceInstaller component and set the StartType property to the appropriate value.

ü Manual service After installation, you must start manually.

ü Automatic When each computer is restarted, the service will start automatically.

ü Disabled service cannot start.

4: Change the Account property of the ServiceProcessInstaller class to localsystem

In this way, whether it is a system that is logged in with the system, the service will start.

Step 4: Generate Services

Generate a project by selecting a generated from the generated menu.

Note Do not run the project by pressing the F5 - cannot run the service item in this way.

Step 5: Installation services

Access the catalog of the compiled executable in the project. Use the output of the project as a parameter, running installutil.exe from the command line. Enter the following code in the command line:

InstallUTIL YourProject.exe

Uninstall service

Use the output of the project as a parameter, running installutil.exe from the command line.

Installutil / U YourProject.exe

At this point, the entire service has been written, compiled, and the installation is complete, you can see the service you wrote in the service of the control panel management tool.

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

New Post(0)