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"?
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.
System.Configuration.Install.ManagedInstallerClass
.InstallHelper(new string[] { "/u", executablePath });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With