vs2013 creating, installing, and debugging Windows service programs

Source: Internet
Author: User

Windows services run as a service, which is useful in some cases. The process for creating a Windows service with VS2013 is as follows:

Create the 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, the resulting project file structure. The Program.cs file on the right is the portal, Service1.cs is the service file, and 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)

namespace windowsservice1{public    partial class Service1:servicebase    {public        Service1 ()        {            InitializeComponent ();            The related code when the service is initialized.            base. ServiceName = "Kangrytest";    Set the service name to match the following installation service name        }        protected override void OnStart (string[] args)        {            //service startup-related code.        }        protected override void OnStop ()        {            ///service shutdown related code.        }    }}
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.


Installation Services

7, the project right-click, compile. Copy the Bin/debug file to a folder (assuming the name is Windowsservicetest). Copy the Installutil.exe file from the C:\Windows\Microsoft.NET\Framework\v4.0.30319 to the windowsservicetest. Create two files Install.bat and Uninstall.bat files in this folder, respectively, as follows:

Install.bat

%~dp0installutil.exe%~dp0windowsservice1.exepause
Uninstall.bat

%~dp0installutil/u%~dp0windowsservice1.exepause

Note Before each command, add a%~dp0 that changes the directory to the current directory. If not added, it can be wrong.

8. Right-click on the Install.bat and run as administrator. window8.1 (other versions unknown), run as Administrator, default directory under C:\Windows\system32.
9, win+r-"input services.msc, OK-" find you in the sixth step input displayname, I here is kangrytestdisplayname.


Debugging Services .

10. Debug-"Attach to Process"


11, select your process, click Attach to the process, and then you can be like a normal program for debugging breakpoints.


Uninstall the service.

12, right-click the 7th step to create the Uninstall.bat attachment, run as administrator, you can delete.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

vs2013 creating, installing, and debugging Windows service programs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.