dir.by  
  Search  
Programming, development, testing
Windows Service (using C#)
Create a new application C# Windows Service (.NET Framework)
  Looked at 3319 times    
 Create a new application C# Windows Service (.NET Framework) 
last updated: 18 April 2023
Download my service with the source code:
WindowsService1.zip ...
Size: 34 kilobytes
 
What my service does:
• when the service starts, creates a timer
• the text is written to the file D:\my_service.txt according to the timer (interval 5 seconds)
Step 1. Open Visual Studio
If you do not have Visual Studio installed you need install Visual Studio...
Open Visual Studio 2022
or
Open Visual Studio 2019
Step 2. Create a new application with type "Windows Service (.NET Framework)"
Step 3. Add the code to the file ProjectInstaller.Designer.cs
  File D:\WindowsService1\ProjectInstaller.Designer.cs
...
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // my line 1
...
this.serviceInstaller1.Description = "My First Service demo"; // my line 2
...
this.serviceInstaller1.DisplayName = "MyFirstService.Demo"; // my line 3
...

Step 4. Add the code to the file Service1.cs
  File D:\WindowsService1\Service1.cs
using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;

namespace WindowsService1
{
     public partial class Service1 : ServiceBase
     {
          private 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; //5 seconds
               _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 filepath = "D:\\my_service.txt";

               // will write text in end of file
               using (StreamWriter fileStream = File.Exists(filepath) ? File.AppendText(filepath): File.CreateText(filepath))
               {
                    fileStream.WriteLine(Message);
               }
          }
     }
}
Step 5. Adding a service to the system
Execute the command:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe D:\WindowsService1\bin\Debug\WindowsService1.exe
 
Open Services and see that the service is installed in the system Windows:
Step 6. Launching the service
Execute the command:
net start MyFirstService.Demo
Step 7. Stop the service
Execute the command:
net stop MyFirstService.Demo
Step 8. Remove the service from the system
Execute the command:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u D:\WindowsService1\bin\Debug\WindowsService1.exe
 
 
Next topic →
C# Windows Service (.NET Framework) with the username. Event when user logs in/out of Windows
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
Technology .NET Framework
Create a new application C# Windows Service (.NET Framework)
C# Windows Service (.NET Framework) with the username. Event when user logs in/out of Windows
Create folder "My Application" under folder "Application and Services logs" | Event Viewer
Technology .NET Core
Create a new application C# Windows Service (library .NET Core and use Worker)
Loading parameters from appsettings.json for application C# Windows Service (library .NET Core and using Worker)
How to open a Notepad app from C# Windows Service | library .NET Core and use Worker
Doing publish a project (compiling and assembling a project) "C# Windows Service" | library .NET Core using Worker
How to register your exe file as Windows Service. Start/stop your Windows Service on the system Windows | library .NET Core using Worker

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика