C # create a Windows Service and install it,

Source: Internet
Author: User

[Switch to] C # create a Windows Service and install it,

This document describes how to create a windows service.

Create a project
  1. 1

    Create a windows Service Project

  2. 2

    Right-click Service1.cs and view the code for compiling the operation logic code.

  3. 3

    In the code, OnStart is used to execute service events. Generally, the thread-based execution method is used to facilitate the execution of a service event

    END
Install service configuration
  1. 1

    Open the Service1.cs View Interface

  2. 2

    Right-click the view and choose --> add installer.

  3. 3

    The ProjectInstaller. cs file is added to the project. Two components are automatically added to the project as follows.

    ServiceProcessInstaller1

    ServiceInstaller1

  4. 4

    Select the serviceProcessInstaller1 component, view attributes, and set the account to LocalSystem.

  5. 5

    Select the serviceInstaller1 component and view its attributes.

    Set the ServiceName value, which indicates the name of the system service.

    Set StartType. If it is Manual, it is manually started. The default value is stopped. If it is Automatic, it is automatically started.

    Set Description and add service Description

  6. 6

    Regenerate a project

    END
Install services
  1.  

    Click Start and Enter cmd in the running process to obtain the command prompt.

    Win7 must be started as an administrator; otherwise, it cannot be installed.

  2.  

    Enter cd C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 and press Enter.

    Switch the current directory. Note that in C: \ Windows \ Microsoft. there are many similar versions under the. NET \ Framework directory. The specific directory depends on the running environment of the project. For example. net framework2.0 requires entering cd C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727

  3.  

    Enter InstallUtil.exe E: \ TestApp \ Winform \ WinServiceTest \ bin \ Debug \ WinServiceTest.exe and press Enter.

     

     

    Note: E: \ TestApp \ Winform \ WinServiceTest \ bin \ Debug \ WinServiceTest.exe indicates the location of the exe file generated by the project.

  4.  

    Open the service and you will see the installed Service.

    END
Uninstall Service
  1. 1

    Uninstalling is simple. Open cmd and enter SC delete WinServiceTest.

    END
Notes
  • When debugging or double-clicking the exe file, the service cannot be started.

  • When you enable cmd in win7, you must enable it as an administrator.

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.