In C#, a Windows Service is a background application that runs without a user interface. It is designed to handle long-running processes independently of user interaction. Windows Services are ideal for tasks like monitoring folders, managing network services, or performing scheduled tasks. These services start automatically when the system boots up and run in the background, providing essential functionality without user intervention. Implementing a Windows Service in C# involves creating a class that inherits from System.ServiceProcess.ServiceBase and overriding methods like OnStart and OnStop to define the service's behavior. Windows Services offer a robust way to manage critical system processes efficiently.
How to create a Windows service in C#?
Now create a Window Service in C# Visual Studio. (I am using Visual Studio 2022)
Step 1 : -
Open Visual Studio and click on Create a new project option :
Step 2 : Now search window service in search bar and choose Template and click on Next Button :
Step 3: Now will open Configure your new project window >> Set your project name and Location of your project and Click on Next Button
Step :4 : Wait a while your project is getting ready . Now you will see below attached Screen .
Step 5 : Right-click on the blank area and select "Add Installer."
Before you can run a Windows Service, you need to install the Installer, which registers it with the Service Control Manager.
How to Add an Installer to a Windows Service :
Step 6 ; After Adding Installer, ProjectInstaller will add to your project, and ProjectInstaller.cs file will be open. Don't forget to save everything
your solution folder looks like below Attached Screen And click on ProjectInstaller.cs
Step 7 : will open a window like below attached screen :
Step 8 : Right-click on the blank area and select "View Code"

It has Constructor, which contains the InitializeComponent method:
The InitializeComponent method contains the logic which creates and initializes the user interface objects dragged on the forming surface and provides the Property Grid of Form Designer.
Note : Don't ever try to call any method before the call of the InitializeComponent process.

Step 9 : Select the InitializeComponent method and press the F12 key to go definition
Now add the below line:
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
You also can add a description and display the service name (optionally).
this.serviceInstaller1.Description = "My First Service demo";
this.serviceInstaller1.DisplayName = "MyFirstService.Demo";

Step 10 : In this step, we will implement a timer and code to call the service at a given time. Then insert data into database .( you may right your code according to your requirement) I am just witting this for example .
this is my Service1.cs class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace TestWindowService
{
public partial class Service1 : ServiceBase
{
Timer timer = new Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
WriteToFile("Service is started at " + DateTime.Now);
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 5000; //number in milisecinds
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
WriteToFile("Service is recall at " + DateTime.Now);
}
protected override void OnStop()
{
WriteToFile("Service is stopped at " + DateTime.Now);
}
public void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" +
DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
}
}
Above code will execute service on every five second and create a log file
Step 11 : Now Rebuild your application.
Step 12 : Search "Command Prompt" and run as administrator.

Step 13 : Fire the below command in the command prompt and press ENTER.
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

Step 14 : Now Go to your project source folder > bin > Debug and copy the full path of your Windows Service exe file
Right click on your Solution and click on Open folder in file explorer


Step 15 : Installing a Windows Service
Open the command prompt and fire the below command and press ENTER.
Syntax
InstallUtil.exe + Your copied path + \your service name + .exe
Our path
InstallUtil.exe C:\TestWindowService\bin\Debug\TestWindowService.exe

Check the status of a Windows Service.
Open services by following the below steps:
- Press the Window key + R.
- Type services.msc
- Find your Service.
Check Windows Service Output
Go to your debug folder there is a folder named Logs : C:\WindowsService1\bin\Debug\Logs
and there will be txt file .
Your service is running
To Install Service Just add /u between the InstallUtil.exe and your Service path
Like this InstallUtil.exe /u [FullPath\ServiceName.exe]