1. Create a New Windows Service Project
- Open Visual Studio and create a new project
- Template selection: Visual C# → Windows → Windows Service
- Set the project name and save path, then click OK
Note: Newer Visual Studio versions no longer include this built‑in template. It is only supported for .NET Framework; for .NET Core/.NET 5+, the
Worker Servicetemplate is recommended.
2. Project Structure Overview
Core auto‑generated files:
- Program.cs: Application entry point
- Service1.cs: Main service logic class
1. Program.cs Entry Code
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}Code language: JavaScript (javascript)
Purpose: Starts the service host and loads one or more service instances.
2. Service1.cs Main Service Class
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
// Executed when the service starts
protected override void OnStart(string[] args)
{
}
// Executed when the service stops
protected override void OnStop()
{
}
}
OnStart: Triggered on service startup, place your business logic hereOnStop: Triggered on service shutdown, used for resource cleanup
3. Add Installer
This step is mandatory! Without it you cannot register as a system service.
Operation Steps
- On the
Service1designer view, Right‑click → Add Installer - Visual Studio auto‑generates
ProjectInstaller.cswith two core components:serviceInstaller1serviceProcessInstaller1

Component Property Configuration
- serviceInstaller1 (Service Information Configuration)
ServiceName: Internal service identifier (used in code)DisplayName: Name shown in service managerDescription: Service descriptionStartType: Startup typeManual / Automatic
- serviceProcessInstaller1 (Run‑as Account Configuration)
Account: Execution identityLocalSystem: Local system account (highest privileges, recommended for testing)User: Specified Windows account (default, requires username and password)
4. Implement Test Business Logic
Your business code belongs inside Service1. Place your custom logic here. Since Windows services are hard to debug, we will test by writing log output as a temporary workaround. Installation and uninstallation are required.
Modify Service1.cs
using System.IO;
protected override void OnStart(string[] args)
{
// Write log on startup
File.AppendAllText("log.txt", "Startup Time:" + DateTime.Now + Environment.NewLine);
}
protected override void OnStop()
{
// Write log on shutdown
File.AppendAllText("log.txt", "Stop Time:" + DateTime.Now + Environment.NewLine);
}Code language: JavaScript (javascript)
5. Compile, Install and Start the Service
- Build the project and get
WindowsServiceDemo.exe - Register service using
InstallUtil.exe(Administrator CMD)
# Install service
InstallUtil.exe WindowsServiceDemo.exe
# Uninstall service
InstallUtil.exe /u WindowsServiceDemo.exeCode language: PHP (php)
- Open service manager
services.msc, locate your service, start / stop it and check log output.
Services cannot run like regular applications. You must use dedicated tools for installation.
Important Notes
- No direct F5 debugging: Windows services cannot be debugged directly. Recommended approach: implement dual‑mode logic; run as console app from command‑line, run as service after installation.
- File path issues: Relative paths do not work reliably; prefer absolute paths.
- Permission issues: The running account determines access to disks, networks and shared folders.
- Startup timeout: Long‑running blocking code inside
OnStartwill trigger system startup failure. Offload heavy tasks onto new threads.
Your first Windows Service