C # create Windows service series 1: Create a Windows service that can be installed, started, stopped, and uninstalled

Source: Internet
Author: User
Tags example of manual

Because windows service is frequently used recently, but the best way to manually deploy is to use the BAT file, but when the program is to be deployed on another server, the path is a problem: You can also use global variables to control the path.

An example of manual deployment is provided.
SetupServer. bat

 
Echo off
If exist "% SystemRoot % Microsoft. NETFramework 2.0.50727" goto netOld
: DispError
Echo. net FrameWork 2.0 is not installed on your machine
Pause
Goto LastEnd
: NetOld
Cd % SystemRoot % Microsoft. NET Framework v2. 0.50727
Echo your machine has installed the corresponding. net FrameWork. you can install this service and press any key to continue ............
Echo off
Pause
% SystemRoot % Microsoft. NET Framework v2.0.50727 installutil C: TradeMatchService2007 TradeMatchService2007 bin Debug TradeMatchService2007. exe
Net start "servTradeMatch2007"
: LastEnd
Rem exit

Uninstall as follows:
UnInstallServer. bat

 
Echo off
If exist "% SystemRoot % Microsoft. NETFramework 2.0.50727" goto netOld
: DispError
Echo your machine has not installed. net FrameWork 2.0. installation is about to terminate
Pause
Goto LastEnd
: NetOld
ECHO is about to uninstall this service. Press any key to continue ............
Echo off
Pause
Net stop "servtradematch2007"
Cd % SystemRoot % Microsoft. NET Framework V2. 0.50727
% SystemRoot % Microsoft. NET Framework v2.0.50727 installutil/uninstall C: tradematchservice2007 tradematchservice2007 bin debug tradematchservice2007. exe
: Lastend
Rem exit
 

You can also determine more than two net FrameWork versions.

Here we will introduce a complete installation program, so the service itself will be omitted. If you need to learn, please contact me
3w@live.cn

Okay, let's get down to the truth.
Assuming that windows servicehas been written, the service name is servtradematch2007,executable file name is tradematchservice2007.exe

 

1. Create a project for installation and deployment in the solution, right-click the project name, and choose [add] --> [project output] from the menu.
In the displayed "add project output group" window, the first drop-down box lists other projects in the solution,
Select the windows service Project to be installed, select the primary output, and click [OK].

 


Add the following four files under "File System on the target computer" -- "application folder"
110000msiexec.exe
2. start. bat
3. stop. bat
4. Main output file

 

In which msiexec.exe is located under WINDOWS \ System32
The content of the stopbat file is as follows:

Echo off
Net stop "servtradematch2007"
Exit

The content of the startbat file is as follows:

 
Echo off
Net start "servTradeMatch2007"
Exit

In the "user's Program folder", create a folder named "vegetable net full-text retrieval service" and create shortcuts for the first three files here.

 

Right-click the "uninstall service" button (the shortcut corresponding to msexec.exe)

Enter a string of Values

 

The corresponding and numerical values after "/X" come from
Setup_tradematchservice2007.vdproj File

 

OK! Generate the installation package,

Don't worry. After installation, Windows service is not installed as a service.
You must also add custom operations to the installation project. Right-click the installation project and choose View> Custom operation.
For example, add custom operations separately. In the application folder, select the primary output you just added.

 

 

The installation process is as follows:

 

 

 

 

Uninstall:

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.