public void UninstallNotInstalledService() { string serviceName = "MyServer"; Assert.False(InstallerServices.IsInstalled(serviceName), "The service must not be installed."); ServiceCommands commands = new ServiceCommands(); bool uninstalled = commands.Uninstall(serviceName); // verify that the service has NOT been tried to be uninstalled Assert.False(uninstalled, "The service uninstaller failed to recognize a not installed service."); }
public static void Main(string[] args) { #region Parse command-line arguments // TODO: if the argument parsing gets too complex better use a specialized library List<string> arguments = new List<string>(); arguments.AddRange(args); if ((arguments.Count > 0) && (arguments[0] == "DaemonNT")) { arguments.RemoveAt(0); } if ((arguments.Count < 1)) { PrintUsage(); return; } ServiceCommands commands = new ServiceCommands(); bool waitAtFinishEnabled = false; // non-mandatory option parameters while ((arguments.Count > 0) && arguments[0].StartsWith("-")) { if (arguments[0].StartsWith("--config-file=")) { commands.ConfigFile = arguments[0].Split(new[] { '=' }, 2)[1]; } else if (arguments[0] == ("-w")) { waitAtFinishEnabled = true; } arguments.RemoveAt(0); } if (arguments.Count < 1) { PrintUsage(); return; } string command = arguments[0]; string serviceName = string.Empty; if (command != "list") { if (arguments.Count == 2) { serviceName = arguments[1]; } else { PrintUsage(); return; } } #endregion #region Execute the requested command try { System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); switch (command) { case "run": commands.Run(serviceName); break; case "debug": commands.DebugStart(serviceName); break; case "install": commands.Install(serviceName); break; case "uninstall": commands.Uninstall(serviceName); break; case "start": commands.Start(serviceName); break; case "stop": commands.Stop(serviceName); break; case "restart": commands.Restart(serviceName); break; case "status": CheckStatus(commands, serviceName); break; case "list": ListServices(commands); break; default: PrintUsage(); return; } } catch (SecurityException) { Console.Error.WriteLine(string.Format( "Error: the '{0}' command requires administrator privileges.", command)); ExitWithStatus(-2, waitAtFinishEnabled); } catch (Exception ex) { Console.Error.WriteLine("Error: {0}", ex.Message); ExitWithStatus(-1, waitAtFinishEnabled); } WaitAtFinish(waitAtFinishEnabled); #endregion }
public void UninstallAlreadyInstalledService() { string serviceName = "MyServer"; Assert.True(InstallerServices.IsInstalled(serviceName), "The service must be installed."); ServiceCommands commands = new ServiceCommands(); bool uninstalled = commands.Uninstall(serviceName); // verify that the service has been uninstalled Assert.True(uninstalled, "The service has not been uninstalled."); Assert.False(InstallerServices.IsInstalled(serviceName), "The service is still installed."); }