【问题标题】:Can not get "StartMode" of a Windows Service. VB.NET无法获取 Windows 服务的“StartMode”。 VB.NET
【发布时间】:2012-12-20 18:21:27
【问题描述】:

我想知道服务启动模式是否设置为“自动”,如果设置为“手动”

我有这个代码:

        If objService("StartMode").ToString = "Automatic" Then
            objService.ChangeStartMode("Manual")
        End If

但是当我编译我的项目时,Visual Studio 会报告该错误:

COMException 未处理:未找到成员。 (来自 HRESULT 的异常:0x80020003 (DISP_E_MEMBERNOTFOUND))

If objService("StartMode").ToString = "Automatic" Then

请帮我解决这个问题?这对我来说非常重要。谢谢。

我在管理员帐户上使用 Visual Studio 2008、VB.NET、Windows XP Sp3。

【问题讨论】:

  • 将 strComputer 调暗为字符串 = "." Dim objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dim colServiceList = objWMIService.ExecQuery _ ("Select * from Win32_Service where Name = '" & iobit & "'")'其中iobit是定义我的服务名称的字符串(例如:wauserv)'MsgBox(iobit.ToString) For Each objService In colServiceList Application.DoEvents() If objService("StartMode").ToString = "Automatic" Then ' If grade = 1然后 objService.ChangeStartMode("Manual") End If
  • 您使用 WMI 而不是 ServiceController 类的任何原因?

标签: vb.net windows-services comexception


【解决方案1】:
    If objService("StartMode").ToString = "Automatic" Then

您正在使用后期绑定,这是一种使用 COM 对象的方式,当您不正确地使用它们的属性和方法时,往往会给您带来运行时错误。您可以通过使用 System.Management 命名空间中的类来避免此类问题。

该语句并没有像您认为的那样做,它调用接口的默认属性并将“StartMode”作为参数传递给该默认属性的属性获取器。这不是该接口的正确用法, StartMode 本身就是一个属性,它不是默认属性。修复:

    If objService.StartMode = "Automatic" Then

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多