c# - Win32Exception:指定的服务不作为已安装的服务存在
问题描述
我正在开发 Windows 服务。在catch
停止服务时阻止出现异常。
System.InvalidOperationException:“在计算机上未找到 Service AirService”
InnerException- Win32Exception:指定的服务不作为已安装的服务存在。
这是我的代码
catch (Exception ex)
{
//WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("AirService"))
{
serviceController.Stop();
}
}
如何检查服务是否已安装?
解决方案
您可以从 ServiceController.GetServices() 获得已安装服务的列表。
public static bool CheckServiceInstalled(string serviceToFind)
{
ServiceController[] servicelist = ServiceController.GetServices();
foreach (ServiceController service in servicelist)
{
if (service.ServiceName == serviceToFind)
return true;
}
return false;
}
推荐阅读
- typescript - TypeScript 条件类型约束在 v3.5.1 之后不再起作用
- sql - 基于排序顺序但在 PostgreSQL 中具有相同名称的列
- laravel - 我的 ubuntu 16 上出现作曲家内存不足错误
- php - 递归循环遍历多维数组并跟踪父数组
- r - 使用我的数据框的子集创建 R 条形图
- excel - Excel 不会自动更新功能
- javascript - HTTP 函数以代码 16 结尾,文档未更新
- python - 用不同的列集连接 Pandas 数据框
- mysql - java.net.ConnectException:连接被拒绝:连接。数据夹
- express - 开玩笑/超级测试错误 - 'instanceof' 的右侧不可调用