【问题标题】:Win32Exception: The specified service does not exist as an installed serviceWin32Exception:指定的服务不作为已安装的服务存在
【发布时间】:2020-03-04 12:11:48
【问题描述】:

我正在开发 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();
    }
}

如何检查服务是否安装?

【问题讨论】:

    标签: c# windows windows-services


    【解决方案1】:

    您从 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;
        }
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 2011-11-26
      • 2016-12-30
      • 1970-01-01
      • 2011-05-13
      相关资源
      最近更新 更多