Add installer for C # Windows Service

Source: Internet
Author: User

Recently, I have been engaged in Windows Services and have a lot of experience. I feel that permissions are definitely much more restrictive than general programs, but they are also more convenient. For example, the system is not blocked in the background, and user logon is not required. Haha, it's a long journey. Today, let's talk about how to create an installation package for the Windows service. Why install the installation package? Of course it is convenient. You don't need to call InstallUtil every time. Also, it looks more formal.

Let's talk about it more. Let's take a look at how to do it. 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)

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 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 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.


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

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


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

In the displayed dialog box, select "primary output" and "Release XXX". This is determined by the Project attributes. Generally, select Release Any CPU. (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:

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

Right-click to install the project, select "View"-& gt; "Custom operations", 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)


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. In fact, as long as the installation, submission, rollback, and uninstallation Add the main output of the project, the service functions can be automatically registered/uninstalled without relying on installutil.exe (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)


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. 


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: 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.