Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically remove a service using C# [duplicate]

Possible Duplicate:
How to install a windows service programmatically in C#?

Is there a way to programmatically remove a service using C# without having to execute "InstallUtil.exe /u MyService.exe"?

like image 769
Bender the Greatest Avatar asked Aug 30 '12 16:08

Bender the Greatest


3 Answers

You can use the ServiceInstaller.Uninstall method in System.ServiceProcess.dll. For example:

ServiceInstaller ServiceInstallerObj = new ServiceInstaller(); 
InstallContext Context = new InstallContext("<<log file path>>", null); 
ServiceInstallerObj.Context = Context; 
ServiceInstallerObj.ServiceName = "MyService"; 
ServiceInstallerObj.Uninstall(null); 

This method will attempt to stop the service first before uninstalling.

like image 94
Mike Christensen Avatar answered Nov 05 '22 19:11

Mike Christensen


System.Configuration.Install.ManagedInstallerClass
                            .InstallHelper(new string[] { "/u", executablePath });
like image 1
L.B Avatar answered Nov 05 '22 19:11

L.B


Services are listed in the Windows Registry under HKLM\SYSTEM\CurrentControlSet\services. If you remove the key corresponding to the service's given name (not the display name; the one under which it was registered), you will have effectively "unregistered" the service. You can do this programmatically with the Microsoft.Win32.Registry object. You will need CAS permissions on the executing computer to modify registry entries.

like image 1
KeithS Avatar answered Nov 05 '22 19:11

KeithS