First, create a service
1, file-"New-" Project-"Windows desktop-" Windows services, modify the name of the project you want. I do not change the name, still called WindowsService1, OK.
2, where the Program.cs file is the portal, Service1.cs is the service file, all the logic is here. Service1.cs consists of two parts, part designer, where you can add a variety of components. Part of the background file, which can write some logic, the default contains 3 methods: constructors, OnStart and OnStop, you can also add OnPause and OnContinue methods.
3. Modify the Service1.cs file as follows (you can add some logic code yourself)
namespacewindowsservice1{ Public Partial classService1:servicebase { PublicService1 () {InitializeComponent (); Base. ServiceName ="kangrytest";//Set the service name to match the following installation service name } protected Override voidOnStart (string[] args) { //The related code when the service starts. } protected Override voidOnStop () {//The related code when the service is closed. } }}
4, double-click the Service1.cs file, go to the design page, right click on the blank-"Add the installer."
5, in the design interface of the installer, there are two components. Click ServiceProcessInstaller1, and in the property bar in the lower right corner, change account to LocalSystem
6, select ServiceInstaller1, the lower right corner of the property box, the ServiceName to modify the 3rd step of the ServiceName, other self-choice. Delayedautostart indicates whether to delay startup after booting. Description represents a description of the service, and DisplayName represents the service display name. Servicesdependedon represents a dependent service item. StartType indicates the startup type, which is divided into auto-start, manual start and disable.
Second, installation Services
Select Project Right click "Generate" to generate EXE file;
The Installutil.exe file is then copied from the C:\Windows\Microsoft.NET\Framework\v4.0.30319 to the build directory (bin/ The debug purpose is to make Installutil.exe and Dp0WindowsService1.exe in the same level directory). To create a new "install. bat" file in this directory, open it with Notepad and enter the following command:
%~dp0installutil.exe%~dp0windowsservice1.exe
Pause
Note Before each command, add a%~dp0 that changes the directory to the current directory. If not added, it can be wrong. pause must change line, otherwise error.
Finally, double-click Install. bat file to complete the service registration.
On my Computer, right-click on "Manage" and open "services" under "services and applications" to see our registered service.
Third, uninstall services
To create a new "uninstall. Bat" file in this directory, open it with Notepad and enter the following command:
%~dp0installutil/u%~dp0windowsservice1.exe
Pause
The same pause will have to be wrapped.
Four, commissioning
Select "Debug"-"Attach to Process" in VS2013, as follows:
This will allow you to debug.
VS2013 Creating a Windows service