編輯

共用方式為


ServiceController.GetServices Method

Definition

Retrieves the non-device driver services on a computer, and those that are not drivers.

Overloads

GetServices(String)

Retrieves all the services on the specified computer, except for the device driver services.

GetServices()

Retrieves all the services on the local computer, except for the device driver services.

GetServices(String)

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs

Retrieves all the services on the specified computer, except for the device driver services.

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()

Parameters

machineName
String

The computer from which to retrieve the services.

Returns

An array of type ServiceController in which each element is associated with a service on the specified computer.

Exceptions

An error occurred when accessing a system API.

The machineName parameter has invalid syntax.

Remarks

GetServices returns only the non-device driver services and the services that are not drivers from the specified computer. To retrieve device driver services, call the GetDevices method. Together, the two methods provide access to all the services on a computer.

See also

Applies to

GetServices()

Source:
ServiceController.cs
Source:
ServiceController.cs
Source:
ServiceController.cs

Retrieves all the services on the local computer, except for the device driver services.

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()

Returns

An array of type ServiceController in which each element is associated with a service on the local computer.

Exceptions

An error occurred when accessing a system API.

Examples

The following example uses the ServiceController class to display the services that are running on the local computer.

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

Remarks

GetServices returns only the non-device driver services and the services that are not drivers from the local computer. To retrieve device driver services, call the GetDevices method. Together, the two methods provide access to all the services on a computer.

See also

Applies to