【问题标题】:Uninstall C# Windows Service - Using uninstaller卸载 C# Windows 服务 - 使用卸载程序
【发布时间】:2010-01-25 17:20:08
【问题描述】:

我已经为我的 Windows 服务创建了一个安装项目。 它安装得很好,但是,如果我卸载项目(通过添加/删除程序,或右键单击 VS - Uninstall 中的安装项目),它似乎不会删除该服务。

我必须在命令行中使用 sc delete 来执行此操作,然后重新启动。

我是不是设置错了?

【问题讨论】:

  • 我们的建议没有提供任何帮助吗? stackoverflow.com/questions/1560407/…
  • @Jon Seigel,对我来说似乎是一个不同的问题。在原版中,他询问如何让他的服务显示在服务面板中。在这里,他询问如何使其显示在添加/删除程序列表(或程序以及现在所称的任何内容)中。
  • 嗯,是的 - 但它不包括卸载......它说对于 windows 2000 将需要重新启动 - 但这是 windows 7!
  • @Sam:说明包括卸载所需的自定义操作。
  • Jon Siegel 链接的说明对我有用。以前我只有“安装自定义操作”下的“主要输出”。将主输出添加到卸载也很有效,因此安装程序的添加/删除程序删除完全删除了服务。

标签: c# windows-services


【解决方案1】:

在您的 Installer 类(您从自定义操作中调用)中,确保覆盖 UnInstall 方法并调用 <pathToFramework>\InstallUtil.exe /u <pathToServiceExe> 以卸载服务。

【讨论】:

    【解决方案2】:

    我不确定在安装程序项目中是否有一种简单的方法可以做到这一点,但这是我们在代码中的做法。当您在命令行中传递“/uninstall”时,我们的服务会在内部进行卸载。

    public static readonly string UNINSTALL_REG_KEY = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
    public static readonly string UNINSTALL_REG_GUID = "{1C2301A...YOUR GUID HERE}";
    
    using (RegistryKey parent = Registry.LocalMachine.OpenSubKey(UNINSTALL_REG_KEY, true))
    {
        try
        {
            RegistryKey key = null;
    
            try
            {
                key = parent.OpenSubKey(UNINSTALL_REG_GUID, true);
                if (key == null)
                {
                    key = parent.CreateSubKey(UNINSTALL_REG_GUID);
                }
    
                Assembly asm = typeof (Service).Assembly;
                Version v = asm.GetName().Version;
                string exe = "\"" + asm.CodeBase.Substring(8).Replace("/", "\\\\") + "\"";
    
                key.SetValue("DisplayName", DISPLAY_NAME);
                key.SetValue("ApplicationVersion", v.ToString());
                key.SetValue("Publisher", "Company Name");
                key.SetValue("DisplayIcon", exe);
                key.SetValue("DisplayVersion", v.ToString(2));
                key.SetValue("URLInfoAbout", "http://www.company.com");
                key.SetValue("Contact", "support@company.com");
                key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd"));
                key.SetValue("UninstallString", exe + " /uninstallprompt");
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(
                "An error occurred writing uninstall information to the registry.  The service is fully installed but can only be uninstalled manually through the command line.",
                ex);
        }
    }
    

    【讨论】:

      【解决方案3】:

      查看 MDSN 上的 ServiceInstaller 文档。您添加一个继承自 System.Configuration.Install.Installer 的类,创建一个 ServiceInstaller 并将该 ServiceInstaller 添加到 Installers 属性中。

      完成此操作后,安装程序项目应该能够为您完成安装和卸载。

      安装程序类示例:

      /// <summary>
      /// The installer class for the application
      /// </summary>
      [RunInstaller(true)]
      public class MyInstaller : Installer
      {
          /// <summary>
          /// Constructor for the installer
          /// </summary>
          public MyInstaller()
          {
              // Create the Service Installer
              ServiceInstaller myInstaller = new ServiceInstaller();
              myInstaller.DisplayName = "My Service";
              myInstaller.ServiceName = "mysvc";
      
              // Add the installer to the Installers property
              Installers.Add(myInstaller);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-08-06
        • 2018-02-01
        • 2011-10-25
        • 1970-01-01
        • 2011-12-10
        • 1970-01-01
        • 2018-11-27
        • 1970-01-01
        • 2017-08-09
        相关资源
        最近更新 更多