** Si se requiere para ser hecho usando solamente la configuración, siga:
Esto puede ser manejado por la aplicación explícita de la eliminación del servicio actual (desinstalar) y luego permitiendo que la versión más reciente de instalar. Para esto, necesitamos actualizar ProjectInstaller.Designer.cs de la siguiente manera:
Considere agregar la siguiente línea al comienzo de InitializeComponent() que desencadena un evento para desinstalar el servicio existente antes de que su instalador actual intente instalar el servicio nuevamente . Aquí desinstalamos el servicio si ya existe.
Añadir según espacios de nombres:
using System.Collections.Generic;
using System.ServiceProcess;
Añadir debajo de la línea de código como se ha descrito antes:
this.BeforeInstall += new
System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);
Ejemplo:
private void InitializeComponent()
{
this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "This is my service name description";
this.serviceInstaller1.ServiceName = "MyServiceName";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[]{
this.serviceProcessInstaller1,
this.serviceInstaller1
}
);
}
El código siguiente llamada por el evento será entonces desinstalar el servicio si existe
void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());
foreach (ServiceController s in services)
{
if (s.ServiceName == this.serviceInstaller1.ServiceName)
{
ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
ServiceInstallerObj.Context = Context;
ServiceInstallerObj.ServiceName = "MyServiceName";
ServiceInstallerObj.Uninstall(null);
break;
}
}
}
PS: Junto con los cambios mencionados, también, por favor considere la actualización de la versión de configuración, CódigoProducto (y optionall UpgradeCode) para una buena práctica, una mejor gestión de versiones, seguimiento y mantenimiento
Para que quede claro, hay un Nombre del servicio y un Nombre para mostrar del servicio. A veces las personas olvidan que la columna "nombre" en services.msc es el nombre para mostrar del servicio. Obtiene el nombre del servicio haciendo clic con el botón derecho en sus servicios y yendo a las propiedades. – Tung
@Tung Lo comprobé, y ambos son lo mismo. –
¿Migrar a http://www.superuser.com? Esta no es realmente una pregunta de programación. – CodingWithSpike