2009-07-28 20 views

Respuesta

7

Puede usar installutil.

Desde la línea de comandos:

installutil YourWinService.exe 

Esta utilidad se instala con el .NET Framework

+0

Creo que aún necesita para crear un instalador en su proyecto para installutil para trabajar con la configuración de un servicio de propiedades: http://www.developer.com/net/net/article.php/11087_2173801_2 –

+2

Dan es correcto, aún necesita crear un instalador. El comando sc (ver a continuación) le permitirá instalar/eliminar/iniciar/detener, etc. un servicio de Windows sin necesidad de un instalador (lo que a mí me parece el núcleo de la pregunta). Esto es útil porque gran parte de los metadatos del servicio (tipo de inicio, nombre de cuenta, propiedades de reinicio, etc.) que se procesan en el instalador se pueden almacenar externamente. Esto es doblemente útil cuando se combina con scripts para implementación, digamos MSBuild o Nant, porque no requiere una recompilación. También significa que puede instalar si está escrito en C#, C, C++. –

+1

Necesita una clase 'Installer', pero no un instalador en el sentido de un setup.exe – Nathan

8

Usted podría tratar de las ventanas sc command

C:\WINDOWS\system32>sc create

DESCRIPCIÓN: SC es un programa de línea de comando usado para commu nicarse con el NT Service Controller y los servicios.

8

Incluyo una clase que hace la instalación por mí. Llamo a la aplicación usando parámetros de línea de comando para instalar o desinstalar la aplicación. También en el pasado incluí un aviso al usuario sobre si deseaban que el servicio se instalara cuando se iniciaba directamente desde la línea de comando.

Aquí está la clase que utilizo:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Diagnostics; 
using Microsoft.Win32; 

namespace [your namespace here] 
{ 
    class IntegratedServiceInstaller 
    { 
     public void Install(String ServiceName, String DisplayName, String Description, 
      System.ServiceProcess.ServiceAccount Account, 
      System.ServiceProcess.ServiceStartMode StartMode) 
     { 
      System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller(); 
      ProcessInstaller.Account = Account; 

      System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller(); 

      System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext(); 
      string processPath = Process.GetCurrentProcess().MainModule.FileName; 
      if (processPath != null && processPath.Length > 0) 
      { 
       System.IO.FileInfo fi = new System.IO.FileInfo(processPath); 
       //Context = new System.Configuration.Install.InstallContext(); 
       //Context.Parameters.Add("assemblyPath", fi.FullName); 
       //Context.Parameters.Add("startParameters", "Test"); 

       String path = String.Format("/assemblypath={0}", fi.FullName); 
       String[] cmdline = { path }; 
       Context = new System.Configuration.Install.InstallContext("", cmdline); 
      } 

      SINST.Context = Context; 
       SINST.DisplayName = DisplayName; 
       SINST.Description = Description; 
       SINST.ServiceName = ServiceName; 
      SINST.StartType = StartMode; 
      SINST.Parent = ProcessInstaller; 

      // http://bytes.com/forum/thread527221.html 
//   SINST.ServicesDependedOn = new String[] {}; 

      System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary(); 
      SINST.Install(state); 

      // http://www.dotnet247.com/247reference/msgs/43/219565.aspx 
      using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true)) 
      { 
       try 
       { 
        Object sValue = oKey.GetValue("ImagePath"); 
        oKey.SetValue("ImagePath", sValue); 
       } 
       catch (Exception Ex) 
       { 
//     System.Console.WriteLine(Ex.Message); 
       } 
      } 

     } 
     public void Uninstall(String ServiceName) 
     { 
      System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller(); 

      System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext("c:\\install.log", null); 
      SINST.Context = Context; 
       SINST.ServiceName = ServiceName; 
      SINST.Uninstall(null); 
     } 
    } 
} 

Y así es como yo lo llamo:

const string serviceName = "service_name"; 
const string serviceTitle = "Service Title For Services Control Panel Applet"; 
const string serviceDescription = "A longer description of what the service does. This is used by the services control panel applet"; 
// Install 
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller(); 
Inst.Install(serviceName, serviceTitle, serviceDescription, 
    // System.ServiceProcess.ServiceAccount.LocalService,  // this is more secure, but only available in XP and above and WS-2003 and above 
    System.ServiceProcess.ServiceAccount.LocalSystem,  // this is required for WS-2000 
    System.ServiceProcess.ServiceStartMode.Automatic); 
// Uninstall 
IntegratedServiceInstaller Inst = new IntegratedServiceInstaller(); 
Inst.Uninstall(serviceName); 
+0

Esto funciona como un encanto. Muy útil. ¡Gracias por publicar! –

Cuestiones relacionadas