using System.ComponentModel; using System.Configuration.Install; using System.Diagnostics; using System.IO; using System.ServiceProcess; namespace serviceInstaller { [RunInstaller(true)] public class ProjectInstaller : Installer { private ServiceProcessInstaller processInstaller; private ServiceInstaller serviceInstaller; public ProjectInstaller() { processInstaller = new ServiceProcessInstaller { Account = ServiceAccount.LocalSystem }; serviceInstaller = new ServiceInstaller { ServiceName = "Log4GraylogService", DisplayName = "Log 4 Graylog Service", Description = "Service for monitoring and sending log files to Graylog", StartType = ServiceStartMode.Automatic }; Installers.Add(processInstaller); Installers.Add(serviceInstaller); } public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); string assemblyPath = Context.Parameters["assemblypath"]; string exePath = Path.Combine(Path.GetDirectoryName(assemblyPath), "console_log4graylog.exe"); using (ServiceController sc = new ServiceController(serviceInstaller.ServiceName)) { if (sc.Status == ServiceControllerStatus.Stopped) { Process.Start("sc", $"config \"{serviceInstaller.ServiceName}\" binPath= \"{exePath}\""); Process.Start("sc", $"start \"{serviceInstaller.ServiceName}\""); } } } } }