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.