How to Install or Uninstall a Windows Service in C#?
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:
- 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.
- 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.
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.
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.
Step 3: To install the service you have to run the command with the following structure:
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:
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.
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:
Installutil.exe \U “C:\test.exe”.
Please Login to comment...