ServiceController.GetServices 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索计算机上的非设备驱动程序服务和不是驱动程序的服务。
重载
GetServices(String) |
检索指定计算机上的所有服务(设备驱动程序服务除外)。 |
GetServices() |
检索本地计算机上的所有服务(设备驱动程序服务除外)。 |
GetServices(String)
检索指定计算机上的所有服务(设备驱动程序服务除外)。
public:
static cli::array <System::ServiceProcess::ServiceController ^> ^ GetServices(System::String ^ machineName);
public static System.ServiceProcess.ServiceController[] GetServices (string machineName);
static member GetServices : string -> System.ServiceProcess.ServiceController[]
Public Shared Function GetServices (machineName As String) As ServiceController()
参数
- machineName
- String
要从中检索服务的计算机。
返回
ServiceController 类型的数组,其中每个元素均与指定计算机上的一个服务关联。
例外
访问 API 时出错。
machineName
参数的语法无效。
注解
GetServices 仅返回非设备驱动程序服务和不是来自指定计算机的驱动程序的服务。 若要检索设备驱动程序服务,请调用 GetDevices 该方法。 这两种方法一起提供对计算机上的所有服务的访问权限。
另请参阅
适用于
GetServices()
检索本地计算机上的所有服务(设备驱动程序服务除外)。
public:
static cli::array <System::ServiceProcess::ServiceController ^> ^ GetServices();
public static System.ServiceProcess.ServiceController[] GetServices ();
static member GetServices : unit -> System.ServiceProcess.ServiceController[]
Public Shared Function GetServices () As ServiceController()
返回
ServiceController 类型的数组,其中每个元素均与本地计算机上的一个服务关联。
例外
访问 API 时出错。
示例
以下示例使用 ServiceController 类显示在本地计算机上运行的服务。
array<ServiceController^>^scServices = ServiceController::GetServices();
// Display the list of services currently running on this computer.
Console::WriteLine( "Services running on the local computer:" );
for each (ServiceController^ scTemp in scServices)
{
if ( scTemp->Status == ServiceControllerStatus::Running )
{
// Write the service name and the display name
// for each running service.
Console::WriteLine();
Console::WriteLine( " Service : {0}", scTemp->ServiceName );
Console::WriteLine( " Display name: {0}", scTemp->DisplayName );
// Query WMI for additional information about this service.
// Display the start name (LocalSystem, etc) and the service
// description.
ManagementObject^ wmiService;
String^ objPath;
objPath = String::Format( "Win32_Service.Name='{0}'", scTemp->ServiceName );
wmiService = gcnew ManagementObject( objPath );
if ( wmiService )
{
wmiService->Get();
Object^ objStartName = wmiService["StartName"];
Object^ objDescription = wmiService["Description"];
if ( objStartName )
{
Console::WriteLine( " Start name: {0}", objStartName->ToString() );
}
if ( objDescription )
{
Console::WriteLine( " Description: {0}", objDescription->ToString() );
}
}
}
}
ServiceController[] scServices;
scServices = ServiceController.GetServices();
// Display the list of services currently running on this computer.
Console.WriteLine("Services running on the local computer:");
foreach (ServiceController scTemp in scServices)
{
if (scTemp.Status == ServiceControllerStatus.Running)
{
// Write the service name and the display name
// for each running service.
Console.WriteLine();
Console.WriteLine(" Service : {0}", scTemp.ServiceName);
Console.WriteLine(" Display name: {0}", scTemp.DisplayName);
// Query WMI for additional information about this service.
// Display the start name (LocalSystem, etc) and the service
// description.
ManagementObject wmiService;
wmiService = new ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'");
wmiService.Get();
Console.WriteLine(" Start name: {0}", wmiService["StartName"]);
Console.WriteLine(" Description: {0}", wmiService["Description"]);
}
}
Dim scServices() As ServiceController
scServices = ServiceController.GetServices()
' Display the list of services currently running on this computer.
Console.WriteLine("Services running on the local computer:")
Dim scTemp As ServiceController
For Each scTemp In scServices
If scTemp.Status = ServiceControllerStatus.Running Then
' Write the service name and the display name
' for each running service.
Console.WriteLine()
Console.WriteLine(" Service : {0}", scTemp.ServiceName)
Console.WriteLine(" Display name: {0}", scTemp.DisplayName)
' Query WMI for additional information about this service.
' Display the start name (LocalSystem, etc) and the service
' description.
Dim wmiService As ManagementObject
wmiService = New ManagementObject("Win32_Service.Name='" + scTemp.ServiceName + "'")
wmiService.Get()
Console.WriteLine(" Start name: {0}", wmiService("StartName"))
Console.WriteLine(" Description: {0}", wmiService("Description"))
End If
Next scTemp
注解
GetServices 仅返回非设备驱动程序服务和不是本地计算机的驱动程序的服务。 若要检索设备驱动程序服务,请调用 GetDevices 该方法。 这两种方法一起提供对计算机上的所有服务的访问权限。