2010-01-25 136 views
2

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

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

我设置了错误吗?

+0

难道我们的意见没有提供任何这方面的帮助? http://stackoverflow.com/questions/1560407/c-windows-service-not-appearing-in-services-list-after-install – 2010-01-25 17:22:57

+0

@Jon Seigel,看起来像对我来说是一个不同的问题。在原来的时候,他问的是如何让他的服务在服务面板上显示出来。在这里,他问如何使它显示在添加/删除程序列表(或程序和现在称为的任何程序)中。 – 2010-01-25 17:27:05

+0

好吧,是的 - 但它不包括卸载.... 它说,对于Windows 2000将需要重新启动 - 但这是Windows 7! – Alex 2010-01-25 17:35:02

回答

0

我不确定在安装程序项目中是否有简单的方法来完成此操作,但以下是代码中的操作方法。我们的服务在内部通过命令行传递“/ 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", "[email protected]"); 
      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); 
    } 
} 
2

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

0

查看关于MDSN的ServiceInstaller文档。您添加一个从System.Configuration.Install.Installer继承的类,创建一个ServiceInstaller并将该ServiceInstaller添加到安装程序属性。

一旦你这样做了,安装程序项目应该能够负责为你安装和卸载。安装程序类的

例子:

/// <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); 
    } 
}