简体   繁体   English

如何使用自定义卸载程序卸载Windows服务

[英]How to uninstall Windows service using Custom Uninstaller

We have many questions on stackoverflow regarding uninstallation of Windows Service. 关于卸载Windows服务,我们对stackoverflow有很多疑问。
After trying all of them I still fail to uninstall the windows service on new version Installation. 尝试了所有这些后,我仍然无法在新版本的安装上卸载Windows服务。

I use setup and deployment project to install/uninstall my project which has a windows service and some other projects. 我使用安装和部署项目来安装/卸载我的项目,该项目有一个Windows服务和一些其他项目。

During installation of newer version, all other projects are successfully re-installed but windows service project fails to Re-install and says: 在安装较新版本的过程中,所有其他项目都已成功重新安装,但Windows服务项目无法重新安装,并说:

Error 1001: The specified service already exists. 错误1001:指定的服务已存在。

I referred this link and tried to add code to my Install custom action to Stop the service. 我引用了此链接并尝试将代码添加到我的安装自定义操作以停止服务。 If I understood the answer in this link correctly, I have put the code to stop the service inside projectInstaller.cs file of the Service: 如果我正确理解了这个链接的答案,我已经把代码停在服务的projectInstaller.cs文件中的服务:

public override void Install(IDictionary stateSaver)
{ 
      ServiceController sc = new ServiceController("SareeManagerNotifications");
      if (sc.Status == ServiceControllerStatus.Running)
          sc.Stop();
     base.Install(stateSaver);
}

Custom action pane looks like: 自定义操作窗格如下所示: 在此输入图像描述

Where the highlighted part is the Service. 突出显示的部分是服务。

I also went through this answer which says to set custom action condition as 我也经历了这个答案 ,它说将自定义动作条件设置为
NOT PREVIOUSVERSIONSINSTALLED . NOT PREVIOUSVERSIONSINSTALLED

在此输入图像描述

This doesn't work for me. 这对我不起作用。 Where am I going wrong ? 我哪里错了?

Thanks in advance :) 提前致谢 :)

Try the following steps while editing the windows service: 编辑Windows服务时,请尝试以下步骤:

  1. Right click your SetupProject from visual studio and select custom actions from view . 右键单击visual studio中的SetupProject,然后从视图中选择自定义操作

  2. You will find 4 custom actions install,commit,rollback and uninstall . 您将找到4个自定义操作安装,提交,回滚和卸载 Right click on each of these actions one after the other and add a custom action 右键单击其中一个操作,然后添加自定义操作

  3. After doing so you will find a select item in project window. 完成后,您将在项目窗口中找到一个选择项 In the window select Application folder from look in dropdown. 在窗口从外观在下拉列表中选择应用程序文件夹 This will list the primary output from windows service , select this and click ok. 这将列出Windows服务主要输出 ,选择此项并单击“确定”。

  4. Save and build the setup project. 保存并构建安装项目。

Hope this will solve your issue.... 希望这能解决你的问题....

Usually this is done through service control operations . 通常这是通过服务控制操作完成的 Basically, you need the Stop and Delete flags set for uninstall. 基本上,您需要为卸载设置Stop和Delete标志。

Another approach would be to use ServiceInstaller.Uninstall to remove the service. 另一种方法是使用ServiceInstaller.Uninstall来删除服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM