/// <summary> /// Install an executable as a service. /// </summary> /// <param name="assemblyPath">The path to the executable.</param> /// <param name="serviceName">The name of the service.</param> /// <param name="displayName">THe display name of the service.</param> /// <param name="description">The description of the service.</param> /// <param name="startType">The startup type.</param> /// <param name="userName">The username to run as.</param> /// <param name="password">The password of the user.</param> /// <param name="dependancies"></param> public static void InstallService(string assemblyPath, string serviceName, string displayName, string description, ServiceStartMode startType, string userName = "", string password = "", IEnumerable<string> dependancies = null) { using (var procesServiceInstaller = new ServiceProcessInstaller()) { if (string.IsNullOrEmpty(userName)) { procesServiceInstaller.Account = ServiceAccount.LocalSystem; } else { procesServiceInstaller.Account = ServiceAccount.User; procesServiceInstaller.Username = userName; procesServiceInstaller.Password = password; } using (var installer = new ServiceInstaller()) { var cmdline = new[] { string.Format("/assemblypath={0}", assemblyPath) }; var context = new InstallContext(string.Empty, cmdline); installer.Context = context; installer.DisplayName = displayName; installer.Description = description; installer.ServiceName = serviceName; installer.StartType = startType; installer.Parent = procesServiceInstaller; if (dependancies != null) { installer.ServicesDependedOn = dependancies.ToArray(); } IDictionary state = new Hashtable(); try { installer.Install(state); installer.Commit(state); } catch (Exception ex) { installer.Rollback(state); throw new Exception("Failed to install the service.", ex); } } } }
static void Install(bool install, InstallerOptions options) { var spi = new ServiceProcessInstaller(); var si = new ServiceInstaller(); spi.Account = ServiceAccount.NetworkService; if (options != null && options.IsUser) { spi.Account = ServiceAccount.User; if (options.UserName != null) { spi.Username = options.UserName; } if (options.Password != null) { spi.Password = options.Password; } } si.StartType = ServiceStartMode.Automatic; si.ServiceName = "CloudBackup"; si.DisplayName = "Cloud Backup Service"; si.Description = "Schedules, run and manage cloud backup"; si.Parent = spi; string path = Assembly.GetEntryAssembly().Location; Console.WriteLine("Location : " + path); var ic = new InstallContext(); ic.Parameters.Add("assemblypath", path); si.Context = ic; spi.Context = ic; IDictionary rb = install ? new Hashtable() : null; try { Console.WriteLine("Starting Default Installation"); if (install) { si.Install(rb); } else { si.Uninstall(rb); } } catch (Exception ex) { log.Fatal(ex); if (rb != null) { Console.WriteLine("Rollback Default Installation"); IDictionary rbc = rb; rb = null; si.Rollback(rbc); } } finally { if (rb != null) { Console.WriteLine("Commit Default Installation"); si.Commit(rb); } } }