[Switch to] C # create a Windows Service and install it,
This document describes how to create a windows service.
Create a project
- 1
Create a windows Service Project
- 2
Right-click Service1.cs and view the code for compiling the operation logic code.
- 3
In the code, OnStart is used to execute service events. Generally, the thread-based execution method is used to facilitate the execution of a service event
END
Install service configuration
- 1
Open the Service1.cs View Interface
- 2
Right-click the view and choose --> add installer.
- 3
The ProjectInstaller. cs file is added to the project. Two components are automatically added to the project as follows.
ServiceProcessInstaller1
ServiceInstaller1
- 4
Select the serviceProcessInstaller1 component, view attributes, and set the account to LocalSystem.
- 5
Select the serviceInstaller1 component and view its attributes.
Set the ServiceName value, which indicates the name of the system service.
Set StartType. If it is Manual, it is manually started. The default value is stopped. If it is Automatic, it is automatically started.
Set Description and add service Description
- 6
Regenerate a project
END
Install services
-
Click Start and Enter cmd in the running process to obtain the command prompt.
Win7 must be started as an administrator; otherwise, it cannot be installed.
-
Enter cd C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 and press Enter.
Switch the current directory. Note that in C: \ Windows \ Microsoft. there are many similar versions under the. NET \ Framework directory. The specific directory depends on the running environment of the project. For example. net framework2.0 requires entering cd C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727
-
Enter InstallUtil.exe E: \ TestApp \ Winform \ WinServiceTest \ bin \ Debug \ WinServiceTest.exe and press Enter.
Note: E: \ TestApp \ Winform \ WinServiceTest \ bin \ Debug \ WinServiceTest.exe indicates the location of the exe file generated by the project.
-
Open the service and you will see the installed Service.
END
Uninstall Service
- 1
Uninstalling is simple. Open cmd and enter SC delete WinServiceTest.
END
Notes
When debugging or double-clicking the exe file, the service cannot be started.
When you enable cmd in win7, you must enable it as an administrator.