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"?
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 });
If what you are trying to do is to uninstall a service, you've written, from within itself and you've added an installer to the project, you can simply instantiate your Installer class and call Uninstall. For example, if you dragged an installer onto the designer service and named that component "ProjectInstaller", you can get your service to uninstall itself with the following code:
var installer = new ProjectInstaller();
installer.Uninstall(null);
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.