C#从windows服务获取结果
大家早上好 我正在开发一个C#WinForms简单应用程序来管理“Apache”windows服务,我可以启动、停止和其他操作 我的设想:C#从windows服务获取结果,c#,windows,service,C#,Windows,Service,大家早上好 我正在开发一个C#WinForms简单应用程序来管理“Apache”windows服务,我可以启动、停止和其他操作 我的设想: 我打开apache配置文件并更改任何行,我将其设置为导致错误配置(我知道) 当我尝试启动Apache服务时,这无法启动,因为配置文件语法不正确(我知道) 来自服务的消息错误已在Windows事件查看器中注册(我知道) 我想在我的C#WinForms应用程序中获取此消息,是否可行 我的代码: public void ManageService(string
- 我打开apache配置文件并更改任何行,我将其设置为导致错误配置(我知道)
- 当我尝试启动Apache服务时,这无法启动,因为配置文件语法不正确(我知道)
- 来自服务的消息错误已在Windows事件查看器中注册(我知道)
- 我想在我的C#WinForms应用程序中获取此消息,是否可行
public void ManageService(string serviceName, int Operation)
{
ServiceController sc = new ServiceController();
sc.ServiceName = serviceName;
try
{
switch (Operation)
{
case 1: sc.Stop(); ; break;
case 2: sc.Start(); break;
}
}
catch (InvalidOperationException e)
{
}
}
如何修改此代码以捕获来自apache服务的消息
警察局。对不起,如果我的英语很差;) 好吧,不是每个人都能回答,但我不知道如何将其放入评论中。请注意WaitForStatus的使用以及错误消息的属性。这完全是赤裸裸的
using System;
using System.Linq;
using System.ServiceProcess;
namespace Revenue.Common.Utility
{
public class WindowsServices
{
private string _ErrorMessage;
public string ErrorMessage { get { return _ErrorMessage; } }
/// <summary>
/// Stop a Windows service service name
/// </summary>
/// <param name="ServiceName"></param>
/// <remarks>
/// A service does not stop instantly, so WaitForStatus method
/// is used to 'wait' until the service has stopped. If the
/// caller becomes unresponsive then there may be issues with
/// the service stopping outside of code.
/// </remarks>
public void StopService(string ServiceName)
{
ServiceController sc = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == ServiceName);
if (sc == null)
return;
if (sc.Status == ServiceControllerStatus.Running)
{
try
{
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped);
}
catch (InvalidOperationException e)
{
_ErrorMessage = e.Message;
}
}
}
/// <summary>
/// Start a Windows service by service name
/// </summary>
/// <param name="ServiceName"></param>
public void StartService(string ServiceName)
{
ServiceController sc = ServiceController.GetServices().FirstOrDefault(s => s.ServiceName == ServiceName);
if (sc == null)
return;
sc.ServiceName = ServiceName;
if (sc.Status == ServiceControllerStatus.Stopped)
{
try
{
sc.Start();
sc.WaitForStatus(ServiceControllerStatus.Running);
}
catch (InvalidOperationException)
{
_ErrorMessage = e.Message;
}
}
}
}
}
使用系统;
使用System.Linq;
使用System.ServiceProcess;
命名空间Revenue.Common.Utility
{
公共类窗口服务
{
私有字符串错误消息;
公共字符串ErrorMessage{get{return}ErrorMessage;}
///
///停止Windows服务名称
///
///
///
///服务不会立即停止,因此WaitForStatus方法
///用于“等待”直到服务停止。如果
///呼叫方无响应,则可能存在以下问题:
///服务在代码之外停止。
///
公共void StopService(字符串ServiceName)
{
ServiceController sc=ServiceController.GetServices().FirstOrDefault(s=>s.ServiceName==ServiceName);
如果(sc==null)
返回;
if(sc.Status==ServiceControllerStatus.Running)
{
尝试
{
sc.停止();
sc.WaitForStatus(服务控制器Status.Stopped);
}
捕获(无效操作异常)
{
_ErrorMessage=e.消息;
}
}
}
///
///按服务名称启动Windows服务
///
///
public void StartService(字符串ServiceName)
{
ServiceController sc=ServiceController.GetServices().FirstOrDefault(s=>s.ServiceName==ServiceName);
如果(sc==null)
返回;
sc.ServiceName=ServiceName;
如果(sc.Status==ServiceControllerStatus.Stopped)
{
尝试
{
sc.Start();
sc.WaitForStatus(服务控制器Status.Running);
}
捕获(无效操作异常)
{
_ErrorMessage=e.消息;
}
}
}
}
}
您是否尝试在catch中捕获异常并将消息返回给调用者?@Karen Payne,我尝试在catch中使用e.message,但这从未调用,我尝试使用WaitForStatus,但我的表单是无限冻结的,即使在这里使用它,也没有问题,因此我没有任何建设性的补充。