/// <summary> /// Used to uninstall a service. /// </summary> public void UninstallService() { var startInfo = new ProcessStartInfo(InstallerCommand) { WindowStyle = ProcessWindowStyle.Normal }; var ops = new WindowsServices(); var statusStates = new[] { "Running", "Stopped" }; if (statusStates.Contains(ops.Status(ServiceName))) { startInfo.Arguments = $"/u {Path.Combine(ServiceFolder, ServiceExecutableName)}"; var p = Process.Start(startInfo); p.WaitForExit(); if (ops.Status(ServiceName) == "Not installed") { MessageBox.Show("Service has been uninstalled"); } } }
/// <summary> /// Start our service. /// First check to see if the service is running as attempting /// to install while running leads to doom and gloom :-) /// Install after the above check /// </summary> public void InstallService() { var startInfo = new ProcessStartInfo(InstallerCommand) { WindowStyle = ProcessWindowStyle.Normal }; var ops = new WindowsServices(); if (ops.Status(ServiceName) == "Running") { startInfo.Arguments = $"/u {Path.Combine(ServiceFolder, ServiceExecutableName)}"; Process.Start(startInfo); } startInfo.Arguments = $"/i {Path.Combine(ServiceFolder, ServiceExecutableName)}"; Process.Start(startInfo); }