Create a Windows Service Program

Source: Internet
Author: User

1. Create a Windows project and select a "Windows Service" project.

2. Write the code you need in the generated Service1.cs Code, as follows:

 

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. Linq;
Using System. ServiceProcess;
Using System. Text;
Using System. Timers;
Using System. IO;

Namespace WindowsService1
{
Public partial class Service1: ServiceBase
{
Timer timer;

Public Service1 () {InitializeComponent ();}

Protected override void OnStart (string [] args)
{
Timer = new Timer (1000 );
Timer. Elapsed + = new ElapsedEventHandler (timer_Elapsed );
Timer. Start ();
}

Protected override void OnStop ()
{
Timer. Stop ();
Timer. Dispose ();
}

Void timer_Elapsed (object sender, ElapsedEventArgs e)
{
String filePath = AppDomain. CurrentDomain. BaseDirectory + "test.txt ";
StreamWriter sw = null;
If (! File. Exists (filePath ))
{
Sw = File. CreateText (filePath );
}
Else
{
Sw = File. AppendText (filePath );
}
Sw. Write ("Access time:" + DateTime. Now. ToString () + Environment. NewLine );
Sw. Close ();
}
}
}

 

3. Create an installer class Installer1.cs under the project Service1.cs. The Code is as follows:

 

Using System;
Using System. Collections;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Configuration. Install;
Using System. Linq;

Namespace WindowsService1
{
[RunInstaller (true)]
Public partial class Installer1: Installer
{
Private System. ServiceProcess. ServiceProcessInstaller spInstaller;
Private System. ServiceProcess. ServiceInstaller sInstaller;

Public Installer1 ()
{
InitializeComponent ();

// Create the ServiceProcessInstaller object and ServiceInstaller object
This. spInstaller = new System. ServiceProcess. ServiceProcessInstaller ();
This. sInstaller = new System. ServiceProcess. ServiceInstaller ();
// Set the account, user name, and password of the ServiceProcessInstaller object
This. spInstaller. Account = System. ServiceProcess. ServiceAccount. LocalSystem;
This. spInstaller. Password = null;
This. spInstaller. Username = null;
// Set the service name
This. sInstaller. ServiceName = "MyTestWindowsService1 ";
// Set the Service Startup Method
This. sInstaller. StartType = System. ServiceProcess. ServiceStartMode. Automatic;
This. Installers. AddRange (new System. Configuration. Install. Installer [] {this. spInstaller, this. sInstaller });
}
}
}

 

4. Generate the project. The exe file is generated in the bin directory. The corresponding directory is C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 \ installutil.exe, and then the directory of the InstallUtil.exe + exe file to be executed, such as InstallUtil.exe F: \ MyProject \ WindowsService1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe. After successful execution, the name of the added service appears in the Windows service.

How to open the Windows service list: Run --> enter services. msc

Find a service named MyTestWindowsService1 In the list. This is the service you created and installed.

 

5. Start the service. Open the bin \ debug folder and find that a file named test.txt has been generated, which records the time. This indicates that the service has been officially started.

 

6. uninstalling the service is also simple. Open the command line tool and go to C: \ Windows \ Microsoft. run the installutil.exe-u F: \ MyProject \ WindowsService1 \ WindowsService1 \ bin \ Debug \ WindowsService1.exe command.

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.