2009-07-28 13 views

risposta

7

È possibile utilizzare installutil.

Dalla riga di comando:

installutil YourWinService.exe 

Questa utilità viene installata con il .NET Framework

+0

Credo che è ancora necessario per creare un programma di installazione nel progetto per installutil di lavorare con la configurazione di un servizio di properties: http://www.developer.com/net/net/article.php/11087_2173801_2 –

+2

Dan è corretto, è comunque necessario creare un programma di installazione. Il comando sc (vedi sotto) ti permetterà di installare/cancellare/avviare/fermare, ecc. Un servizio di Windows senza richiedere un programma di installazione (che mi sembra comunque il nocciolo della domanda). Questo è utile perché gran parte dei metadati del servizio (tipo di avvio, nome account, proprietà di riavvio, ecc.) Che viene cotta nell'installatore possono essere archiviati esternamente. Questo è doppiamente utile quando combinato con script per la distribuzione, ad esempio MSBuild o Nant, perché non richiede una ricompilazione. Significa anche che è possibile installare se è scritto in C#, C, C++. –

+1

È necessaria una classe 'Installer', ma non un programma di installazione nel senso di un setup.exe – Nathan

8

Si potrebbe provare le finestre sc command

C:\WINDOWS\system32>sc create

DESCRIZIONE: SC è un programma a riga di comando usato per commu migliorando con il controller di servizio NT e i servizi.

8

Includo una classe che esegue l'installazione per me. Chiaro l'applicazione utilizzando i parametri della riga di comando per installare o disinstallare l'app. Ho anche in passato incluso un prompt per l'utente se volevano il servizio installato quando avviato direttamente dalla riga di comando.

Ecco la classe che uso:

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

Ed ecco come lo chiamo io:

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

Funziona come un incantesimo. Molto maneggevole. Grazie per la pubblicazione! –

Problemi correlati