Beispiel #1
0
        public static void ChangeStatus(ServiceResult serviceResult, Action action, IPublicAPI contextAPI)
        {
            if (serviceResult == null)
            {
                throw new ArgumentNullException(nameof(serviceResult));
            }

            if (contextAPI == null)
            {
                throw new ArgumentNullException(nameof(contextAPI));
            }

            try
            {
                var info = new ProcessStartInfo
                {
                    FileName        = "net",
                    Verb            = "runas",
                    UseShellExecute = true,
                    WindowStyle     = ProcessWindowStyle.Hidden,
                };

                if (action == Action.Start)
                {
                    info.Arguments = string.Join(' ', "start", serviceResult.ServiceName);
                }
                else if (action == Action.Stop)
                {
                    info.Arguments = string.Join(' ', "stop", serviceResult.ServiceName);
                }
                else if (action == Action.Restart)
                {
                    info.FileName  = "cmd";
                    info.Arguments = string.Join(' ', "/c net stop", serviceResult.ServiceName, "&&", "net start", serviceResult.ServiceName);
                }

                var process = Process.Start(info);
                process.WaitForExit();
                var exitCode = process.ExitCode;

                if (exitCode == 0)
                {
                    contextAPI.ShowNotification(GetLocalizedMessage(serviceResult, action));
                }
                else
                {
                    contextAPI.ShowNotification("An error occurred");
                    Log.Error($"The command returned {exitCode}", MethodBase.GetCurrentMethod().DeclaringType);
                }
            }
            catch (Win32Exception ex)
            {
                Log.Error(ex.Message, MethodBase.GetCurrentMethod().DeclaringType);
            }
        }