使用C#以编程方式删除服务

Ben*_*est 12 .net c# windows-services

可能重复:
如何在C#中以编程方式安装Windows服务?

有没有办法以编程方式使用C#删除服务而无需执行"InstallUtil.exe/u MyService.exe"?

Mik*_*sen 22

您可以在System.ServiceProcess.dll中使用ServiceInstaller.Uninstall方法.例如:

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

此方法将尝试在卸载之前先停止服务.

  • 对于那些仍在寻找它的人,InstallContext 在 System.Configuration.Install 中 (2认同)