How to install and uninstall Windows services using c#

How to install and uninstall Windows services using c#

In this article, If you are developing a Windows service with the .NET Framework, you can quickly install and uninstall your service app by using the InstallUtil.exe command-line utility.

Start menu, select the Visual Studio <version> directory, then select Developer Command Prompt for VS <version>

Install service:

D:\Services\bin\Debug>installutil Services.exe

D:\Services\bin\Debug>installutil Services.exe
Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the D:\Services\bin\Debug\Services.exe assembly's progress.
The file is located at D:\Services\bin\Debug\Services.InstallLog.
Installing assembly 'D:\Services\bin\Debug\Services.exe'.
Affected parameters are:
   logtoconsole =
   logfile = D:\Services\bin\Debug\Services.InstallLog
   assemblypath = D:\Services\bin\Debug\Services.exe
Installing service My first windows service...
Service My first windows service has been successfully installed.
Creating EventLog source My first windows service in log Application...

The Install phase completed successfully, and the Commit phase is beginning.
See the contents of the log file for the D:\Services\bin\Debug\Services.exe assembly's progress.
The file is located at D:\Services\bin\Debug\Services.InstallLog.
Committing assembly 'D:\Services\bin\Debug\Services.exe'.
Affected parameters are:
   logtoconsole =
   logfile = D:\Services\bin\Debug\Services.InstallLog
   assemblypath = D:\Services\bin\Debug\Services.exe

The Commit phase completed successfully.

The transacted install has completed.

Uninstall service:

 

D:\Services\bin\Debug>installutil /u Service.exe

Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 

The uninstall is beginning.
See the contents of the log file for the D:\Services\bin\Debug\Services.exe assembly's progress.
The file is located at D:\Services\bin\Debug\Services.InstallLog.
Uninstalling assembly 'D:\Services\bin\Debug\Services.exe'.
Affected parameters are:
   logtoconsole =
   logfile = D:\Services\bin\Debug\Services.InstallLog
   assemblypath = D:\Services\bin\Debug\Services.exe
Removing EventLog source My first windows service.
Service My first windows service is being removed from the system...
Service My first windows service was successfully removed from the system.
Attempt to stop service My first windows service.

The uninstall has completed.

D:\Services\bin\Debug>installutil /u Services.exe
Microsoft (R) .NET Framework Installation utility Version 4.8.3752.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 

The uninstall is beginning.
See the contents of the log file for the D:\Services\bin\Debug\Services.exe assembly's progress.
The file is located at D:\Services\bin\Debug\Services.InstallLog.
Uninstalling assembly 'D:\Services\bin\Debug\Services.exe'.
Affected parameters are:
   logtoconsole =
   logfile = D:\Services\bin\Debug\Services.InstallLog
   assemblypath = D:\Services\bin\Debug\Services.exe
Removing EventLog source My first windows service.
Warning: The source My first windows service is not registered on the local machine.
Service My first windows service is being removed from the system...
An exception occurred during the uninstallation of the System.ServiceProcess.ServiceInstaller installer.
System.ComponentModel.Win32Exception: The specified service does not exist as an installed service
An exception occurred while uninstalling. This exception will be ignored and the uninstall will continue. However, the application might not be fully uninstalled after the uninstall is complete.

The uninstall has completed.
An exception occurred while uninstalling. This exception will be ignored and the uninstall will continue. However, the application might not be fully uninstalled after the uninstall is complete.

 

2020-04-16