Open In App

How to Install or Uninstall a Windows Service in C#?

Last Updated : 16 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Windows Services is a core component of the Microsoft Windows NT operating system that allows the creation and regulation of processes. Windows Services can start without user intervention and may continue to run long after the user has logged off. Windows services run in the background of the OS and usually start from the OS startup. Windows services are written in .NET using C# Programming Language. Therefore the language enables us to install/uninstall any Windows service at demand. In this article, we will learn how to install or uninstall a windows service in C#. 

Initial Download Of Microsoft .NET Framework

For this process, we need to have Microsoft .NET Framework installed in the Operating System. If you have C# Installed then you would already have it, otherwise, you could download and install it from Microsoft Website. 

For demonstration, we would be installing a service named tor.exe into the OS, which is located at the path C:\tor.exe.

Find the absolute path of the directory where InstallUtil.exe is present. Which is usually present in the C drive. In our case, the file is located at C:\Windows\Microsoft.NET\Framework\v2.0.50727.

The path will be different for different versions of .NET. You may just want to search on your machine where InstallUtil.exe is stored. For example:

  1. For the 32-bit version of the .NET Framework 4 or 4.5 and later, if your Windows installation directory is C:\Windows, the default path is C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe.
  2. For the 64-bit version of the .NET Framework 4 or 4.5 and later, the default path is C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe

Process

For this purpose, we would be using the InstallUtil.exe preset in the .NET Framework. The goal of the util is to install/uninstall any services in the operating system. Firstly we would be using it to install a service.

Installing the Service 

Step 1: Start Windows Command Processor (cmd.exe) by typing cmd in the start menu and selecting the open/run option. A new window would have appeared, similar to the image shown below. 

open cmd

 

Step 2: Now, go to the parent folder of the installutil.exe. For example, its parent directory would be at C:\Windows\Microsoft.NET\Framework\v2.0.50727. 

Run Command: 

cd C:\Windows\Microsoft.NET\Framework\v2.0.50727. 

go to the required installutil.exe to install windows servide in c#

 

Step 3: To install the service you have to run the command with the following structure:

Command Structure: 

InstallUtil.exe “_Path_to_the_Service_”

Where the _Path_to_the_Service_ is the location of the service which we are willing to install. For example, C:\tor.exe. So the command is:

Run Command: 

Installutil.exe “C:\tor.exe”

installing service using, C:\tor.exe path for installutil.exe.

 

Step 4: Now, the service tor has been started and it could be confirmed by the task manager. Type Task manager, and in the search box, you will find a file name tor into your system. 

confirmation of installation, find tor in task manager

 

Uninstalling the Service

Now, we will learn how to uninstall the service. For that, we would use the Uninstall Switch \U. Therefore, the syntax for uninstalling a Service using C# is:

Run Command: 

Installutil.exe \U “C:\test.exe”.

use \U to uninstall, tor.exe by using test.exe.

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads