C # write windows service application

Time:2021-6-13

It is easy to write windows services in. Net framework platform. First, open visual studio and create aWindows servicesAfter creating, you can see a design interface of. CS file. Right click to see several menus. Click Add installation program to go to another design interface of. CS file. There are two components on this interface by default, one is called serviceprocessinstaller1, the other is called serviceinstaller1, The properties window of serviceinstaller1 component is as follows:

C # write windows service application

The description attribute and displayName attribute refer to the description and name of the service respectively. The servicename attribute refers to the process name of the service. The starttype attribute refers to the startup type of the service. Here, automatic means automatic startup; Here, the account property of serviceprocessinstaller1 component will be changed to localsystem. Otherwise, when the service is installed, the dialog box that requires user name and password may appear.

The configuration of the service installation component is finished. Now go to the code location of the service, or in the right-click menu location just now, and click “view code” to see the code to be executed by the service. I write the following code:

using System;
using System.ServiceProcess;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }
        System.Timers.Timer timer = new System.Timers.Timer();
        protected override void OnStart(string[] args)
        {
            timer.Interval = 1000;
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Start();
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timertick);
        }
        private void timertick(object source, System.Timers.ElapsedEventArgs e)
        {
            System.IO.FileStream fs = new System.IO.FileStream("D:\log.txt", System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
            sw.WriteLine(DateTime.Now.ToString());
            sw.Close();
            fs.Close();
        }
        protected override void OnStop()
        {
        }
    }
}

The code in OnStart method will be executed once when the service starts, so here I use a timer to execute infinitely; The code in the onstop method is executed when the service stops. After the service program is compiled, it will still be an. EXE file, but it can’t run directly. Here, we need to use the program provided by the. Net framework to install the service we wrote. The command to install the service is as follows:

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\WindowsService1.exe

Note that the command needs to be executed with administrator privileges. The path of the installutil program may be different. This is determined according to the path of the. Net framework installation. The command to uninstall the service is as follows:

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u D:\WindowsService1.exe

After the service is successfully installed, you can find it in the windows service manager

C # write windows service application