卸载C#Windows服务-使用卸载程序
我已经为我的windows服务创建了一个安装项目。 它安装得很好,但是,如果我卸载项目(通过添加/删除程序,或者右键单击VS-uninstall中的安装项目),它似乎不会删除服务 我必须在命令行中使用sc delete来执行此操作,然后重新启动卸载C#Windows服务-使用卸载程序,c#,windows-services,C#,Windows Services,我已经为我的windows服务创建了一个安装项目。 它安装得很好,但是,如果我卸载项目(通过添加/删除程序,或者右键单击VS-uninstall中的安装项目),它似乎不会删除服务 我必须在命令行中使用sc delete来执行此操作,然后重新启动 我设置错误了吗?我不确定在安装程序项目中是否有一种简单的方法可以做到这一点,但下面是我们如何在代码中做到这一点的。当您在命令行上通过“/uninstall”时,我们的服务在内部执行卸载 public static readonly string UNIN
我设置错误了吗?我不确定在安装程序项目中是否有一种简单的方法可以做到这一点,但下面是我们如何在代码中做到这一点的。当您在命令行上通过“/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);
}
}
在
安装程序
类(通过自定义操作调用)中,确保覆盖卸载
方法并调用\InstallUtil.exe/u
卸载服务。查看MDSN上的文档。添加从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);
}
}
//
///应用程序的安装程序类
///
[运行安装程序(true)]
公共类MyInstaller:安装程序
{
///
///安装程序的构造函数
///
公共MyInstaller()
{
//创建服务安装程序
ServiceInstaller myInstaller=新ServiceInstaller();
myInstaller.DisplayName=“我的服务”;
myInstaller.ServiceName=“mysvc”;
//将安装程序添加到Installers属性
Installers.Add(myInstaller);
}
}
我们的建议对此没有任何帮助吗@乔恩·赛格尔,对我来说是另一个问题。在原文中,他询问如何让他的服务出现在服务面板中。在这里,他问如何使其显示在添加/删除程序列表(或程序以及现在的名称)中。嗯,是的-但不包括卸载。。。。它说windows 2000需要重新启动,但这是windows 7@山姆:说明包括卸载所需的自定义操作。Jon Siegel链接的说明对我有用。以前,我只有安装自定义操作下的“主输出”。添加要卸载的主输出也很有效,因此,删除安装程序的“添加/删除程序”会完全删除该服务。