C # Windows Service Automatic Installation and Registration

Source: Internet
Author: User

First, you must create a Windows service project. Everyone should know how to do this (Leave a message asking me if I don't understand it), and then "add installer" to the service ", 1: (this step is exactly the same as registering with installutil)

(Figure 1)

After adding the package, a serviceprocessinstal1 and serviceinstaller1 will be displayed. If you have two services in a project, there will be two serviceinstaller classes, 2:

(Figure 2)

Figure 3 shows the attribute page of serviceprocessinstal1, where account refers to the account used to run the service. If it is only used locally, you can select LocalSystem (Local System), which is the highest local permission, you can also choose to interact with the desktop. If you want security, select the one that suits you.

(Figure 3)

Figure 4 shows the attribute page of serviceinstaller2, where displayname is the name of the service displayed in the Windows Service Manager, and desciption is the description of the service. The following servicename is the internal name of the service. The most important thing is the following starttype, which refers to the Service Startup method, which is automatic, manual, or disabled.


(Figure 4)

Next is the focus of today's lecture. Click "file"> "add"> "new project" (5)

(Figure 5)

Select "other project type"> "installation and deployment" (6)

(Figure 6)

After the project is created, right-click the newly created installation project and choose View> File System (7)

(Figure 7)

In this case, the file system attribute is opened. As you can see, there are three file systems on the left of the screen, because we are a Windows service program and do not need to place icons in the Start Menu or desktop, so right-click "application folder"> "add"> "project output" (8)

(Figure 8)

In the displayed dialog box, select "primary output" and "Release XXX". This is determined by the Project attributes. Generally, select release any CPU. (9)

(Figure 9)

The system will automatically put the project output file, that is, the Windows Service Program's exe file and all referenced class library files in the middle of the box. To facilitate the installation, we add installutil.exe and two self-written batch files. The add method is similar to the above, just change "project output" to "File ".
Result 10:

(Figure 10)

Next, configure the installation process and let the installer automatically install and uninstall the Windows service.

Right-click the project and choose "View"> "Custom operations". 11:

(Figure 11)

The window on the left is changed to the custom operation view, right-click "Install"-> branch of the worker. Click it. On the properties page on the right, set arguments = "your Windows service file name" and 14 (note that a space is required before the file name)

(Figure 12)


(Figure 13)


(Figure 14)

In the same example, installutil.exe is also available in the submission and dismounting processes. However, in the attribute page, set arguments = "-u your Windows service file name". In this way, the installer can automatically install, register, and uninstall Windows Services. (15)

(Figure 15)

Next, we should compile the Windows Service and installation project at the same time. Right-click solution and select "properties" (16). In the displayed dialog box, click Generate after the installation project. (17)

(Figure 16)


(Figure 17)

Finally, right-click the solution and select "generate solution". At this time, Vs will compile both projects. Note that:The startup sequence of the project must be before the Windows Service Project and after the installation project.

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.