This article introduces Windows Services in .NET and how to create a Windows Service in C# and .NET using Visual Studio.
What is a Windows Service?
Windows Services are non-UI software applications that run in the background. Windows services usually start when an operating system boots and is scheduled to run in the background to execute some tasks. Windows services can also be started automatically or manually. You can also manually pause, stop and restart Windows services.
Windows service is a computer program that runs in the background to execute some tasks. Some examples of Windows services are auto-update of Windows, check emails, print documents, SQL Server Agent, file and folder scanning and indexing, etc. If you open your Task Manager and click on the Services tab, you will see hundreds of services running on your machine. You can also see the statuses of these services. Some services are running, some have paused, and some have stopped. You can start, stop, and pause a service from here by right click on the service.

You may also find all services running on your machine in the following ways:
- Go to Control Panel and select "Services" inside "Administrative Tools."
- Next, open the Run window (Window + R), type services.msc, and press ENTER.
How to create a Windows service in C#?
Let's create a Windows Service in C# using Visual Studio.
Step 1
Open Visual Studio, click File > New, and select a project. Next, select a new project from the Dialog box, select "Window Service," and click the OK button.

Step 2
Go to Visual C# ->" Windows Desktop" ->" Windows Service," give an appropriate name and then click OK.
Once you click the OK button, the below screen will appear, which is your service.
Step 3
Right-click on the blank area and select "Add Installer."
How to Add an Installer to a Windows Service
Before you can run a Windows Service, you need to install the Installer, which registers it with the Service Control Manager.
After Adding Installer, ProjectInstaller will add to your project, and ProjectInstakker.cs file will be open. Don't forget to save everything (by pressing the ctrl + shift + s key)
Solution Explore looks like this:
Step 4
Right-click on the blank area and select "View Code"

Step 5
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.
Very important: Don't ever try to call any method before the call of the InitializeComponent process.
Step 6
Select the InitializeComponent method and press the F12 key to go definition.
Step 7
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 8
In this step, we will implement a timer and code to call the service at a given time. Then, we will create a text file and write the current time in the text file using the service.
Service1.cs class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace MyFirstService {
public partial class Service1: ServiceBase {
Timer timer = new Timer(); // name space(using System.Timers;)
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;
}
protected override void OnStop() {
WriteToFile("Service is stopped at " + DateTime.Now);
}
private void OnElapsedTime(object source, ElapsedEventArgs e) {
WriteToFile("Service is recall 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);
}
}
}
}
}
Code explanation - the above code will call service every 5 seconds, create a folder if none exists, and write our message.
Step 9. Rebuild your application.
Right-click on your project or solution and select Rebuild.
Step 10
Search "Command Prompt" and run as administrator.
Step 11
Fire the below command in the command prompt and press ENTER.
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
Step 12
Now Go to your project source folder > bin > Debug and copy the full path of your Windows Service exe file.


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:\Users\Faisal-Pathan\source\repos\MyFirstService\MyFirstService\bin\Debug\MyFirstService.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.


You may notice that the Windows service is running.

Check Windows Service Output
The service will create a text file with the following text in it.
The log folder will be created in your bin folder.
Uninstalling a Windows Service
If you want to uninstall your service, fire the below command.
- Syntax InstallUtil.exe -u + Your copied path + \your service name + .exe
- Our path InstallUtil.exe -u C:\Users\Faisal-Pathan\source\repos\MyFirstService\MyFirstService\bin\Debug\MyFirstService.exe
Summary
This article taught us 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.
I also uploaded this project on GitHub; here is the URL https://github.com/faisal5170/WindowsService.
balenzi phillipPosted Jul 16, 2025, 12:56 PM
Thank you very much
balenzi phillipPosted Nov 22, 2024, 8:10 AM
Thank you very much.... u saved my day
Faisal PathanPosted Mar 11, 2024, 9:28 AM
You are Welcome!
Duc ParkPosted Dec 9, 2023, 4:32 AM
It works .. thank mr. Pathan .! <3
r rPosted Jun 5, 2023, 9:05 AM
IS it possible to write this rasa run -m models --enable-api --cors "*" in window services
gopalreddy ganugapentaPosted Mar 17, 2023, 12:13 PM
Very useful.
gopalreddy ganugapentaPosted Mar 17, 2023, 12:12 PM
Hi.. very useful and working fine.
Dave DavePosted Mar 7, 2023, 10:46 PM
Very nice and simple explanation. Works perfectly. Initially had some issues installing, but went back and corrected the installer code and it installed properly.
Paul KizilosPosted Sep 22, 2022, 5:26 PM
This is VERY helpful. Thank you!
Mubariz ThakurPosted Nov 22, 2021, 1:48 PM
How to create a .net service that pick up the file from local machine and read the data and store in database.once its store u will get a email notification that the data has been stored successfully ? Pls help ??
The TheoryPosted Aug 16, 2021, 1:16 PM
Whenever I try to install the service, it gives me a Win32 Exception. I'm not sure I understand, because the file is literally right there -- I'm looking at it, and I copied the path, added double quotes, so I don't see why it's not finding it?
West EastPosted Aug 10, 2021, 1:42 AM
Good stuff.How to ensure it automatically starts after installation and always starts everytime computer restarts?
Oscar SchultPosted Jul 1, 2021, 12:26 PM
Excellent article. Saved me a lot of time.
chandrugonda nareshPosted Apr 23, 2021, 5:56 AM
Thanks bro, excellent explanation.
Chaitanya KumarPosted Apr 8, 2021, 1:18 PM
How to access the running service from mvc
Ashvin RamphulPosted Mar 19, 2021, 10:16 AM
Thanks bro, excellent explanation. Great article.
sam kowPosted Jan 18, 2021, 5:57 AM
Million thanks, Great article.
Thilina WilliamsPosted Dec 10, 2020, 11:25 PM
How To fix Below The Error!
Thilina WilliamsPosted Dec 10, 2020, 11:24 PM
Exception occurred while initializing the installation:System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Users\asus\Desktop\Chanuka' or one of its dependencies. The system cannot find the file specified..
FernandoPosted Sep 24, 2020, 12:59 AM
Muchas gracias, me fue de mucha ayuda!
Ayesha MullaPosted Jun 21, 2020, 9:54 AM
Good explanation..Thanks
Emil SimonyanPosted Jun 9, 2020, 9:11 AM
Thanks, excellent work, really help
Mehmet BlackPosted Apr 22, 2020, 8:20 AM
Thanks, great work
RohanPosted Apr 9, 2020, 12:06 PM
Thank you for the article, it's also helpful for folks like me who develop simple Windows Forms apps and is just curious how Windows Services are developed.
Johary RamanoelinaPosted Apr 8, 2020, 1:37 AM
Excellent tutorial. Steps and screenshots are cristal clear.
Nasir SiddiquiPosted Apr 1, 2020, 7:24 AM
I have to call the controller method of Web API through windows service, for this I have created a windows service and installed it, i has been installed successfully. I have also debug the service in Debug mode it is able to call the controller method of Web API, but when I run the service which I have installed, it is not calling the API method, logs are creating as well. Please suggest
tebourbi riadhPosted Mar 11, 2020, 11:25 PM
Thank you Faisal, great!
Arvind ChourasiyaPosted Mar 10, 2020, 8:34 AM
We have to use Developer command prompt to uninstall the service.
Kees KraamerPosted Mar 3, 2020, 3:29 AM
Thank you so much for this article and I see that the typo is already mentioned
quan quanPosted Jan 21, 2020, 7:18 PM
There's a typo in the article: ProjectInstakker.cs instead of ProjectInstaller.cs
Sh YarPosted Jan 16, 2020, 1:45 PM
Thank you Faisal, Simple & Brilliant Article.
Tuman SahuPosted Jan 8, 2020, 2:52 AM
Thankyou for this artical, its great help for my application.
Tuman SahuPosted Jan 8, 2020, 2:52 AM
Hi Faisal,
Isaac VasquezPosted Nov 25, 2019, 11:29 AM
Nice, how can I use a timer in windows Service with Core 2.2? Timer & Threading Timer doesnt work :(
Deeppak ChaoudharyPosted Nov 11, 2019, 4:04 AM
Very nice. good one. Its very easy to learn. Thanks Faisal
srivatsa bsPosted Oct 7, 2019, 12:12 AM
I have created scheduler and its running fine without any error, but now the if i want to control the logic of stop/start of service and configuring the service to run with user defined time interval instead of hard coding the value from code, is that possible
Timothy GrayPosted Oct 3, 2019, 6:41 AM
The following needs to be added to the installer this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; or it asks for a login to install the service
Luciano DbPosted Sep 22, 2019, 8:45 AM
10 minutes to read your post, create project, install and run (fully working) Win Service. Simply... incredible. What else: Thank you a lot :-)
Pius OPosted Sep 13, 2019, 3:40 AM
Hi, Can I use this to run a console or batch file regardless of user login to a server?
shavil shaikPosted Aug 29, 2019, 4:33 AM
Presentation was very good.. i liked it.
Hasitha ChinthakaPosted Aug 22, 2019, 2:38 AM
Nice and simple, awesome work.
Kaustav basuPosted Aug 21, 2019, 5:36 AM
Helpful Explanation
Farid GuseynliPosted Aug 4, 2019, 6:51 AM
Thank you very much!
Shubham KulePosted Aug 1, 2019, 7:37 AM
Hi, Thanks for the article it is very useful.
Marek UherPosted Jun 19, 2019, 5:08 AM
Perfect explanation! It is the first guide that works without any problems for me. Thank you
Raj KPosted Apr 22, 2019, 6:51 AM
Excellent work bro !!
Faisal PathanPosted Apr 15, 2019, 1:31 AM
Just close the Services applet, install the service, and then re-open the Services applet., make sure to run the console as admin.
Yeshal ShahPosted Apr 15, 2019, 1:09 AM
The transacted install has completed. The installation failed, and the rollback has been performed. I got this message after installing in command prompt. what should I do? Please help.
Girija ViswanathanPosted Mar 5, 2019, 10:56 PM
Hi, Thanks for the article it is very useful and I have a doubt how can we copy the file to another file in regular interval. Iam getting error if I call the copy method in OnElapsedTime method
Hamid KhanPosted Jan 1, 2019, 11:55 PM
Very good explanation Thanks...............................
zaw myo heinPosted Sep 13, 2018, 10:08 PM
Thank you.So valuable for me.
Rameshkumar AngaPosted Jul 25, 2018, 3:26 PM
Brother,im fresher in dotnet my question is, how you know timer have override methods please explain
Ezel AmirPosted Jul 23, 2018, 11:45 AM
Excelente tema y tutorial.
Kaushik DudhatPosted Jul 19, 2018, 10:51 PM
Nice article
Viknaraj ManogararajahPosted Jul 14, 2018, 11:07 PM
Nice Article, Thank you for sharing.........
Ravishankar NPosted Jul 12, 2018, 2:18 AM
Nice Article. Thanks for Sharing
Lazy HeapPosted Jul 6, 2018, 12:29 AM
Nice explanation. Just to add one point here. Just need to make sure that if timer event is already in process then another elapsed event may cause issue to access same file handle. This may stop your service. In these cases you'll have to make sure if execution is already writing in file then another elapsed event should not try to access the file.
Hiten PandyaPosted Jun 29, 2018, 11:42 AM
Nice and straightforward presentation.
AKshay RautPosted Jun 29, 2018, 11:25 AM
Please mention which version of visual studio you have used.
ragu moorthiPosted Jun 29, 2018, 10:43 AM
Nice example
Kiranteja JallepalliPosted Jun 29, 2018, 12:49 AM
Awesome presentation