次の方法で共有


ServiceController.GetServices メソッド ()

デバイス ドライバ サービス以外の、ローカル コンピュータのすべてのサービスを取得します。

Overloads Public Shared Function GetServices() As ServiceController()
[C#]
public static ServiceController[] GetServices();
[C++]
public: static ServiceController* GetServices() [];
[JScript]
public static function GetServices() : ServiceController[];

戻り値

ServiceController 型の配列。この配列の各要素は、ローカル コンピュータのサービスに関連付けられています。

例外

例外の種類 条件
Win32Exception システム API へのアクセス中にエラーが発生しました。

解説

GetServices は、ローカル コンピュータからデバイス以外のドライバ サービスおよびドライバ以外のサービスだけを返します。デバイス ドライバ サービスを取得するには、 GetDevices メソッドを呼び出します。2 つのメソッドを組み合わせると、コンピュータ上のすべてのサービスにアクセスできます。

使用例

[Visual Basic, C#, C++] ServiceController クラスを使用して、ローカル コンピュータで実行しているサービスを表示する例を次に示します。

 

         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 (LocalSytem, 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
      

[C#] 
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 (LocalSytem, 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"]);
   }
}

[C++] 
ServiceController * scServices[] = ServiceController::GetServices();

// Display the list of services currently running on this computer.

Console::WriteLine("Services running on the local computer:");
for (int i=0; i<scServices->Length; i++)
{
   ServiceController *scTemp = scServices[i];

   if (scTemp)
   {
      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 (LocalSytem, etc) and the service
         // description.
         ManagementObject *wmiService;
         String *objPath;
   
         objPath = String::Format(S"Win32_Service.Name='{0}'", scTemp->ServiceName);
         wmiService = new ManagementObject(objPath);
         if (wmiService)
         {
            wmiService->Get();

            Object *objStartName = wmiService->get_Item(S"StartName");
            Object *objDescription = wmiService->get_Item(S"Description");
            if (objStartName)
            {
               Console::WriteLine("    Start name:      {0}", objStartName->ToString());
            }
            if (objDescription)
            {
               Console::WriteLine("    Description:     {0}", objDescription->ToString());
            }
         }
      }
   }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

ServiceController クラス | ServiceController メンバ | System.ServiceProcess 名前空間 | ServiceController.GetServices オーバーロードの一覧 | GetDevices