Exemple #1
0
        /**
         * <summary>Stop the web server service(s).</summary>
         * <remarks>Copied from https://docs.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicecontroller.getservices?view=netframework-4.7.2 .</remarks>
         */
        public static void StopWebServerService()
        {
            ServiceController[] scServices;
            scServices = ServiceController.GetServices();

            foreach (ServiceController eachService in scServices)
            {
                string serviceName = eachService.ServiceName.ToLower();
                if (serviceName.Contains("apache") || serviceName.Contains("nginx") || serviceName.Contains("iis") || serviceName.Contains("w3svc"))
                {
                    Console.WriteLine("Found service: " + eachService.ServiceName + " (" + eachService.DisplayName + ")");
                    Console.WriteLine("The " + eachService.ServiceName + " service is currently " + (eachService.Status == ServiceControllerStatus.Running ? "running" : "stopped") + ".");
                    if (eachService.Status == ServiceControllerStatus.Running)
                    {
                        // if service is running.
                        Console.WriteLine("Trying to stop, please wait...");
                        try
                        {
                            eachService.Stop();
                            eachService.WaitForStatus(ServiceControllerStatus.Stopped);
                            AppConsole.ClearCurrentConsoleLine();
                            Console.WriteLine("The " + eachService.ServiceName + " service is now {0}.", eachService.Status.ToString().ToLower());
                        }
                        catch (InvalidOperationException)
                        {
                            AppConsole.ClearCurrentConsoleLine();
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Error.WriteLine("Could not stop the " + eachService.ServiceName + " service.");
                            Console.Error.Close();
                            Console.ResetColor();
                            System.Threading.Thread.Sleep(5000);
                            Environment.Exit(1);
                        }
                    }
                    Console.WriteLine();
                }
            }
        }
Exemple #2
0
        /**
         * <summary>Validate required folders. Exit the program if not found.</summary>
         */
        public static void ValidateRequiredPath()
        {
            if (Fs.IsPhpRunningFolderExists() == false)
            {
                AppConsole.ErrorMessage("Error! The \"php-running\" folder is not exists in the <PHP versions folder>. (" + Fs.PhpDir + ")");
                System.Threading.Thread.Sleep(5000);
                Environment.Exit(1);
                return;
            }

            if (Fs.IsPhpVersionExists(FileSystem.phpVersion) == false)
            {
                AppConsole.ErrorMessage("Error! The selected PHP version does not exists. (" + FileSystem.phpVersion + ")");
                System.Threading.Thread.Sleep(5000);
                Environment.Exit(1);
                return;
            }

            if (Service.IsServiceExists("apache"))
            {
                WsApache = new WebServer.Apache(FileSystem.phpVersion, Fs.ApacheDir);
                WebServer.Apache.ValidateRequiredPath();
            }
        }