private void SendCommand(ServiceCommandEnum serviceCommandEnum) { if (serviceDataGridView.SelectedRows.Count <= 0 && serviceCommandEnum != ServiceCommandEnum.Load) { MessageBox.Show(@"请选择需要操作的服务"); return; } var serviceKey = string.Empty; if (serviceCommandEnum != ServiceCommandEnum.Load) { serviceKey = Convert.ToString(serviceDataGridView.SelectedRows[0].Cells["serviceKey"].Value); } var resultMessage = ManageHelper.SendCommand(serviceKey, serviceCommandEnum); MessageBox.Show(resultMessage); LoadData(); }
public static string SendCommand(string serviceKey, ServiceCommandEnum command) { var sendCommandParm = new Dictionary <string, object>(); var dic = new Dictionary <string, object> { { "ServiceKey", serviceKey }, { "Command", command } }; sendCommandParm.Add("sendCommandParam", dic); try { var sendCommandResult = SendCommand(sendCommandParm); return(sendCommandResult.Message); } catch (Exception ex) { return(ex.Message); } }
public static void ExecuteCommand(string serviceName, ServiceCommandEnum serviceCommand) { System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController(serviceName); switch (serviceCommand) { case ServiceCommandEnum.Start: try { Logging.Log(LogLevelEnum.Info, "Starting service"); serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); Logging.Log(LogLevelEnum.Info, "Service started"); } catch (Exception ex) { Logging.Log(LogLevelEnum.Fatal, "Start failed: " + FileLogger.GetInnerException(ex).Message); MessageBox.Show("Could not start " + Settings.Instance.ServiceDisplayName); } break; case ServiceCommandEnum.Stop: try { Logging.Log(LogLevelEnum.Info, "Stopping service"); serviceController.Stop(); serviceController.WaitForStatus(ServiceControllerStatus.Stopped); Logging.Log(LogLevelEnum.Info, "Service stopped"); } catch (Exception ex) { Logging.Log(LogLevelEnum.Fatal, "Stop failed: " + FileLogger.GetInnerException(ex).Message); MessageBox.Show("Could not stop " + Settings.Instance.ServiceName); } break; case ServiceCommandEnum.Restart: try { Logging.Log(LogLevelEnum.Info, "Restarting service"); serviceController.Stop(); serviceController.WaitForStatus(ServiceControllerStatus.Stopped); serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); Logging.Log(LogLevelEnum.Info, "Service restarted"); } catch (Exception ex) { Logging.Log(LogLevelEnum.Fatal, "Restart failed: " + FileLogger.GetInnerException(ex).Message); MessageBox.Show("Could not restart " + Settings.Instance.ServiceName); } break; case ServiceCommandEnum.Uninstall: try { Logging.Log(LogLevelEnum.Info, "Uninstalling service"); if (ServiceControl.ServiceStatus != ServiceControllerStatus.Running) { serviceController.Start(); serviceController.WaitForStatus(ServiceControllerStatus.Running); } serviceController.ExecuteCommand((int)ServiceCommandEnum.Uninstall); serviceController.Close(); Thread.Sleep(250); File.Delete(Settings.Instance.ServiceFile); Logging.Log(LogLevelEnum.Info, "Service uninstalled"); return; } catch (Exception ex) { Logging.Log(LogLevelEnum.Fatal, "Uninstall failed: " + FileLogger.GetInnerException(ex).Message); MessageBox.Show("Could not uninstall " + Settings.Instance.ServiceName); } break; } serviceController.Close(); }
public static void ExecuteCommand(ServiceCommandEnum serviceCommand) { ExecuteCommand(Settings.Instance.ServiceName, serviceCommand); }
public static void ExecuteCommand(int serviceId, ServiceCommandEnum serviceCommand) { ExecuteCommand(DatabaseController.GetService(serviceId).ServiceName, serviceCommand); }
/// <summary> /// Executes the command. /// </summary> /// <param name="serviceId">The service id.</param> /// <param name="serviceCommand">The service command.</param> public static void ExecuteCommand(int serviceId, ServiceCommandEnum serviceCommand) { ExecuteCommand(DatabaseController.GetService(serviceId, Settings.Instance.LoggerConfiguration).ServiceName, serviceCommand); }