Learn about Windows Service in C#

What is Windows Service?

Windows Service is a special type of program that runs in the background on a Windows operating system. It's like a silent helper that performs tasks without needing any interaction from the user.

How to create Windows Service in C#?

Let's create a Windows Service in C# using Visual Studio.

Step 1. Open Visual Studio, click File ->New, -> select a project. Next, select a new project from the Dialog box, select "Window Service," and click the OK button.

Window Service

Give an appropriate name to the project.

Appropriate name

Once you create the project, the below screen will appear.

Create the project

Step 2. Right-click on blank, and click on “Add Installer”.

Add Installer

After Adding the Installer, ProjectInstaller will add to your project, and the ProjectInstakker.cs file will be open. Don't forget to save everything.

Solution Explorer looks like this,

Solution Explorer

Step 3. Right-click on the blank area and click on View Code.

Blank area

Step 4. It has a Constructor, which contains the InitializeComponent method.

InitializeComponent method

Step 5. Select InitialComponent press F12, And add the below code under the InitialComponent method.

this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem.

and also add service name display name,

Add service name

Step 6. In this step, we will implement a mail-sending service, and create the class EmailService.cs.

Add the below code to it.

Add From mail and password (create a token in Gmail then place it here) add ToMail.

Add ToMail

Step 7. Call the SendMail method from EmailService.Cs class in OnStart() and OnStop() methods as shown below.

SendMail method

Step 8. Rebuild your project.

Right-click on the project and click on Rebuild.

Rebuild your project

step 9. Now Go to your project source folder > bin > Debug and copy the full path of your Windows Service exe file.

Windows Service exe file

Step 10. Installing Windows services.

Open the command prompt and run as administrator.

Enter the below command,

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" "Enter your .exe file path"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" "C:\MyFolders\Work\WindowsServices\WindowsServiceTutorials\WindowsServiceTutorials\bin\Debug\WindowsServiceTutorials.exe"

Step 11. Check service with display name- Mail Sending Service. And start and stop service you will receive emails.

Step 12. to uninstall Windows service in .net.

open the command prompt and run as administrator enter the below command.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "Enter your .exe file path"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe" -u "C:\MyFolders\Work\WindowsServices\WindowsServiceTutorials\WindowsServiceTutorials\bin\Debug\WindowsServiceTutorials.exe"

Summary

By this article, you understood, how to create a Windows Service and install/Uninstall it using InstallUtil.exe from the command prompt.

I hope you found this tutorial easy to follow and understand.

Here I am sharing a GitHub link please go through it

https://github.com/manideep-bestshop/WindowsServiceTutorial