How to install a windows service using windows command prompt in C#?

CsharpServer Side ProgrammingProgramming

Step 1 −

Create a new windows service application.

Step 2 −

For running a Windows Service, you need to install the Installer, which registers it with the Service Control Manager. Right click on the Service1.cs[Design] and Add Installer.

Step 3 −

Right click on the ProjectInstaller.cs [Design] and select the view code.

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
namespace DemoWindowsService{
   [RunInstaller(true)]
   public partial class ProjectInstaller : System.Configuration.Install.Installer{
      public ProjectInstaller(){
         InitializeComponent();
      }
   }
}

Press F12 and go to the implementation of the InitializeComponent class. Add the service name and description which will be the name of the windows service during installation.

private void InitializeComponent(){
   this.serviceProcessInstaller1 = new
   System.ServiceProcess.ServiceProcessInstaller();
   this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
   //
   // serviceProcessInstaller1
   //
   this.serviceProcessInstaller1.Account =
   System.ServiceProcess.ServiceAccount.LocalService;
   this.serviceProcessInstaller1.Password = null;
   this.serviceProcessInstaller1.Username = null;
   //
   // serviceInstaller1
   //
   this.serviceInstaller1.Description = "My Demo Service";
   this.serviceInstaller1.ServiceName = "DemoService";
   //
   // ProjectInstaller
   //
   this.Installers.AddRange(new System.Configuration.Install.Installer[] {
   this.serviceProcessInstaller1,
   this.serviceInstaller1});
}

Step 4 −

Now let us add below logic to write log data in the text file in the Service1.cs class.

using System;
using System.IO;
using System.ServiceProcess;
using System.Timers;
namespace DemoWindowsService{
   public partial class Service1 : ServiceBase{
      Timer timer = new Timer();
      public Service1(){
         InitializeComponent();
      }
      protected override void OnStart(string[] args){
         WriteToFile("Service started at " + DateTime.Now);
         timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
         timer.Interval = 5000;
         timer.Enabled = true;
      }
      protected override void OnStop(){
         WriteToFile("Service stopped at " + DateTime.Now);
      }
      private void OnElapsedTime(object source, ElapsedEventArgs e){
         WriteToFile("Service recall at " + DateTime.Now);
      }
      public void WriteToFile(string Message){
         string path = @"D:\Demo";
         if (!Directory.Exists(path)){
            Directory.CreateDirectory(path);
         }
         string filepath = @"D:\Demo\Log.txt";
         if (!File.Exists(filepath)){
            using (StreamWriter sw = File.CreateText(filepath)){
               sw.WriteLine(Message);
            }
         } else {
            using (StreamWriter sw = File.AppendText(filepath)){
               sw.WriteLine(Message);
            }
         }
      }
   }
}

Step 5 (Installation) −

Now we will install our windows service using command prompt. Open the command prompt as administrator and provide the below command.

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

Open the folder where our windows service exe file is present and run the below command.

InstallUtil.exe C:\Users\[UserName]
source\repos\DemoWindowsService\DemoWindowsService\bin\Debug\
DemoWindowsService.exe

Now open Services from the windows application menu.

We could see that our windows service is installed and started running as expected.

Below output shows that the service is running and wiring the log to the text file as expected.

raja
Published on 24-Sep-2020 16:08:32
Advertisements