以编程方式使用C#删除服务 [英] Programmatically remove a service using C#

查看:135
本文介绍了以编程方式使用C#删除服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/358700/how-to-install-a-windows-service-programmatically-in-c">How在C#编程方式安装Windows服务?

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

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

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.uninstall.aspx">ServiceInstaller.Uninstall方法在System.ServiceProcess.dll。例如:

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.

这篇关于以编程方式使用C#删除服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆