无法通过VB.Net中的服务控制器访问Windows服务描述

时间:2013-04-24 09:39:31

标签: vb.net windows-services servicecontroller

我有来自我公司的Windows服务:

enter image description here

我想从代码中获取服务描述!但由于某种原因,它不会返回服务描述。

任何人都可以帮助我。

这是我的代码,以及它返回的内容:

Dim MyServices As ServiceController = New ServiceController("AccTech Exchange Rate Import")
    Dim Status As String = MyServices.Status.ToString
    Dim Name As String = MyServices.ServiceName

以及MyService变量返回的内容。

enter image description here

我如何从这里获得服务说明?

此致

编辑:

在Miki Shah向我指出正确的方向之后,这是我的代码!

Dim MyServices As ServiceController = New ServiceController("AccTech Exchange Rate Import")

Dim Status As String = MyServices.Status.ToString
Dim Name As String = MyServices.ServiceName
Dim Description As String

Dim objPath As String = String.Format("Win32_Service.Name='{0}'", Name)
Using service As New ManagementObject(New ManagementPath(objPath))
    Description = service("Description")
End Using

1 个答案:

答案 0 :(得分:1)

您可以按照以下方式获取,并且必须添加System.Management的引用

string serviceName = MyServices.ServiceName
string objPath = string.Format("Win32_Service.Name='{0}'", serviceName);
using (ManagementObject service = new ManagementObject(new ManagementPath(objPath)))
{
    Console.WriteLine(service["Description"]);
}