Windows service again

Source: Internet
Author: User
      C # How to Create a Windows Service.

      C # create Windows Services

      Windows Services are called NT Services in earlier versions of Visual Studio, and new names are enabled in vs.net. Using Visual C # To create a Windows service is not difficult. This article will guide you step by step to create a Windows service and use it. When the service starts and stops, it writes some text information to a text file.

      C # create a Windows Service Step 1: create a service framework

      to create a new Windows Service, select the Windows Service option from the Visual C # project, create a new file name for the project, and click OK.

      you can see that the wizard adds the webservice1.cs class to the project file:

      the meaning of each attribute is:

      ◆ autolog

      whether to automatically write log files to the System

      ◆ canhandlepowerevent

      power supply event reception during service

      ◆ canpauseandcontinue

      whether the service accepts the request to suspend or continue running

      ◆ canshutdown

      whether the service receives a notification when the computer that runs it shuts down, so that it can call the onshutdown Process

      ◆ canstop

      whether the service accepts the stop operation request

      ◆ servicename

      service name

      C # create a Windows Service Step 2: Add a function to the service

      In the. CS Code file, we can see two ignored functions onstart and onstop.

      the onstart function is executed when the service is started, and the onstop function is executed when the service is stopped. Here, some text information is written to a text file when the service is started or stopped. The Code is as follows:

 
 
  1. Protected Override VoidOnstart (String[] ARGs)
  2.  
  3. {
  4.  
  5. Filestream FS =NewFilestream (@"D: \ mcwindowsservice.txt",
  6. Filemode. openorcreate, fileaccess. Write );
  7.  
  8. Streamwriter m_streamwriter =NewStreamwriter (FS );
  9.  
  10. M_streamwriter.basestream.seek (0, seekorigin. End );
  11.  
  12. M_streamwriter.writeline ("mcwindowsservice:
  13.  
  14. Service started"+ Datetime. Now. tostring () +"\ N ");
  15.  
  16. M_streamwriter.flush ();
  17.  
  18. M_streamwriter.close ();
  19.  
  20. FS. Close ();
  21.  
  22.  
  23. }
  24.  
  25.  
  26. Protected Override VoidOnstop ()
  27.  
  28. {
  29.  
  30. Filestream FS =NewFilestream (@"D: \ mcwindowsservice.txt",
  31.  
  32. Filemode. openorcreate, fileaccess. Write );
  33.  
  34. Streamwriter m_streamwriter =NewStreamwriter (FS );
  35.  
  36. M_streamwriter.basestream.seek (0, seekorigin. End );
  37.  
  38. M_streamwriter.writeline ("Mcwindowsservice:
  39.  
  40. Service stopped"+ Datetime. Now. tostring () +"\ N");
  41.  
  42. M_streamwriter.flush ();
  43.  
  44. M_streamwriter.close ();
  45.  
  46. FS. Close ();
  47.  
  48. }

C # create a Windows Service Step 3: InstallProgramAdd to service application

Visual Studio. NET comes with the installation component that can be used to install resources associated with service applications. The installation component registers a single service on the system to which it is being installed, and notifies the Service Control Manager of the existence of the service.

To correctly install the service, you do not need to perform any special encoding in the installer. However, to add special features to the installation process, you may occasionally need to modify the content of the installation program.

To add an installer to a service application, follow these steps:

1: In the solution, access the design view of the service to which you want to add and install components.

2: In the Properties window, click Add installer Link

In this case, a new projectinstaller class and two installation components serviceprocessinstaller and serviceinstaller are added to the project, and the service attribute values are copied to the component.

3: To determine how to start the service, click the serviceinstaller component and set the starttype attribute to an appropriate value.

◆ Manual

After the service is installed, you must start it manually.

◆ Automatic

The service is automatically started every time the computer restarts.

◆ Disabled

The service cannot be started.

4: Change the account attribute of the serviceprocessinstaller class to LocalSystem.

In this way, no matter which user is logged on to the system, the service will always start.

C # Step 4 of Windows Service creation: generate a service program

You can select generate from the generate menu to generate a project.

Do not run the project by pressing F5-you cannot run the service project in this way.

C # Step 5 of creating a Windows Service: Install the service

Access the directory of compiled executable files in the project.

Use the project output as a parameter to run installutil.exe from the command line. Enter the following code in the command line:

 
  
  
  1. Installutil yourproject.exe

Uninstall Service

Use the project output as a parameter to run installutil.exe from the command line.

  
  
  1. Installutil/u yourproject.exe

C # learn how to create a Windows service. Here, I hope to help you learn and understand C # How to Create a Windows service.

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.