C # How to Create a Windows Service.
C # create Windows Services
Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in vs.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.
C # create a Windows Service Step 1: create a service framework
to create a new Windows Service, select the Windows Service option from the Visual C # project, create a new file name for the project, and click OK.
you can see that the wizard adds the webservice1.cs class to the project file:
the meaning of each attribute is:
◆ autolog
whether to automatically write log files to the System
◆ canhandlepowerevent
power supply event reception during service
◆ canpauseandcontinue
whether the service accepts the request to suspend or continue running
◆ canshutdown
whether the service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process
◆ canstop
whether the service accepts the stop operation request
◆ servicename
service name
C # create a Windows Service Step 2: Add a function to the service
In the. CS Code file, we can see 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, some text information is written to a text file when the service is started or stopped. The Code is as follows:
- Protected Override VoidOnstart (String[] ARGs)
-
-
-
- {
-
-
-
- Filestream FS =NewFilestream (@"D: \ mcwindowsservice.txt",
-
- Filemode. openorcreate, fileaccess. Write );
-
-
- Streamwriter m_streamwriter =NewStreamwriter (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 VoidOnstop ()
-
-
-
- {
-
-
-
- Filestream FS =NewFilestream (@"D: \ mcwindowsservice.txt",
-
-
- Filemode. openorcreate, fileaccess. Write );
-
-
-
- Streamwriter m_streamwriter =NewStreamwriter (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 ();
-
-
-
- }
C # create a Windows Service Step 3: InstallProgramAdd to service application
Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.
To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.
To add an installer to a service application, follow these steps:
1: In the solution, access the design view of the service to which you want to add and install components.
2: In the Properties window, click Add installer Link
In this case, a new projectinstaller class and two installation components serviceprocessinstaller and serviceinstaller are added to the project, and the service attribute values are copied to the component.
3: To determine how to start the service, click the serviceinstaller component and set the starttype attribute to an appropriate value.
◆ Manual
After the service is installed, you must start it manually.
◆ Automatic
The service is automatically started every time the computer restarts.
◆ Disabled
The service cannot be started.
4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.
In this way, no matter which user is logged on to the system, the service will always start.
C # Step 4 of Windows Service creation: generate a service program
You can select generate from the generate menu to generate a project.
Do not run the project by pressing F5-you cannot run the service project in this way.
C # Step 5 of creating a Windows Service: Install the service
Access the directory of compiled executable files in the project.
Use the project output as a parameter to run installutil.exe from the command line. Enter the following code in the command line:
- Installutil yourproject.exe
Uninstall Service
Use the project output as a parameter to run installutil.exe from the command line.
- Installutil/u yourproject.exe
C # learn how to create a Windows service. Here, I hope to help you learn and understand C # How to Create a Windows service.