ServiceController イベント


関連項目
ServiceController クラスSystem.ServiceProcess 名前空間
ServiceBase クラス
ServiceControllerStatus
ServiceType
ServiceController クラス
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)


ServiceController クラスを使用すると、既存のサービスに接続して、動作を制御できます。ServiceController クラスのインスタンスを作成すると、特定の Windows サービスと対話できるようにプロパティを設定します。この設定後、クラスを使用してサービスの開始、停止、およびその他の操作を実行できます。
ほとんどの場合、ServiceController コンポーネントは管理機能として使用します。たとえば、ServiceController インスタンスを通じてサービスにカスタム コマンドを送信する Windows アプリケーションまたは Web アプリケーションを作成できます。サービス コントロール マネージャ (SCM: Service Control Manager) Microsoft 管理コンソール スナップインではカスタム コマンドがサポートされていないため、このような機能は便利です。
ServiceController のインスタンスを作成した後で、対話の対象となるサービスを識別する 2 つのプロパティとして、制御するコンピュータ名およびサービス名を設定する必要があります。
![]() |
---|
既定では、MachineName はローカル コンピュータに設定されているため、別のコンピュータを指定するインスタンスを設定しない限り、この設定を変更する必要はありません。 |
通常、サービスの作成者は、特定のコマンドに関連付けられているアクションをカスタマイズするコードを書き込みます。たとえば、ServiceBase.OnPause コマンドに応答するコードをサービスに含むことができます。この場合、Pause タスクのカスタム プロセスは、システムがサービスを一時中断する前に実行されます。
サービスが処理できる一連のコマンドは、そのサービスのプロパティによって決まります。たとえば、あるサービスの CanStop プロパティを false に設定するとします。この設定により、そのサービスでは Stop コマンドが使用できなくなります。必要なボタンを無効にすると、SCM でサービスを停止できなくなります。コードでサービスを停止しようとすると、エラーが発生し、" servicename を停止できませんでした。" というエラー メッセージが表示されます。

System.MarshalByRefObject
System.ComponentModel.Component
System.ServiceProcess.ServiceController


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ServiceController メンバ
System.ServiceProcess 名前空間
ServiceBase クラス
ServiceControllerStatus
ServiceType
ServiceController コンストラクタ ()
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)


ServiceController クラスを使用して Alerter サービスが停止しているかどうかを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始し、サービス ステータスが Running に設定されるまで待機します。
' Check whether the Alerter service is started. Dim sc As New ServiceController() sc.ServiceName = "Alerter" Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status) If sc.Status = ServiceControllerStatus.Stopped Then ' Start the service if the current status is stopped. Console.WriteLine("Starting the Alerter service...") Try ' Start the service, and wait until its status is "Running". sc.Start() sc.WaitForStatus(ServiceControllerStatus.Running) ' Display the current service status. Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status) Catch Console.WriteLine("Could not start the Alerter service.") End Try End If
// Check whether the Alerter service is started. ServiceController sc = new ServiceController(); sc.ServiceName = "Alerter"; Console.WriteLine("The Alerter service status is currently set to {0}", sc.Status.ToString()); if (sc.Status == ServiceControllerStatus.Stopped) { // Start the service if the current status is stopped. Console.WriteLine("Starting the Alerter service..."); try { // Start the service, and wait until its status is "Running". sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); // Display the current service status. Console.WriteLine("The Alerter service status is now set to {0}.", sc.Status.ToString()); } catch (InvalidOperationException) { Console.WriteLine("Could not start the Alerter service."); } }
// Check whether the Alerter service is started. ServiceController^ sc = gcnew ServiceController; if ( sc ) { sc->ServiceName = "Alerter"; Console::WriteLine( "The Alerter service status is currently set to {0}", sc->Status ); if ( sc->Status == (ServiceControllerStatus::Stopped) ) { // Start the service if the current status is stopped. Console::WriteLine( "Starting the Alerter service..." ); try { // Start the service, and wait until its status is "Running". sc->Start(); sc->WaitForStatus( ServiceControllerStatus::Running ); // Display the current service status. Console::WriteLine( "The Alerter service status is now set to {0}.", sc->Status ); } catch ( InvalidOperationException^ e ) { Console::WriteLine( "Could not start the Alerter service." ); } } }
// Check whether the Alerter service is started. ServiceController sc = new ServiceController(); sc.set_ServiceName("Alerter"); Console.WriteLine("The Alerter service status is currently set to {0}", sc.get_Status().ToString()); if (sc.get_Status().Equals(ServiceControllerStatus.Stopped)) { // Start the service if the current status is stopped. Console.WriteLine("Starting the Alerter service..."); try { // Start the service, and wait until its status is // "Running". sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); // Display the current service status. Console.WriteLine("The Alerter service status is now set to {0}.", sc.get_Status().ToString()); } catch (InvalidOperationException exp) { Console.WriteLine("Could not start the Alerter service."); } }


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ServiceController コンストラクタ (String)
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)



ServiceController クラスを使用して、Telnet サービスの現在のステータスを確認する例を次に示します。サービスが停止している場合、このコードはサービスを開始します。サービスが実行中である場合は、そのサービスを停止します。
' Toggle the Telnet service - ' If it is started (running, paused, etc), stop the service. ' If it is stopped, start the service. Dim sc As New ServiceController("Telnet") Console.WriteLine("The Telnet service status is currently set to {0}", sc.Status) If sc.Status.Equals(ServiceControllerStatus.Stopped) Or sc.Status.Equals(ServiceControllerStatus.StopPending) Then ' Start the service if the current status is stopped. Console.WriteLine("Starting the Telnet service...") sc.Start() Else ' Stop the service if its status is not set to "Stopped". Console.WriteLine("Stopping the Telnet service...") sc.Stop() End If ' Refresh and display the current service status. sc.Refresh() Console.WriteLine("The Telnet service status is now set to {0}.", sc.Status)
// Toggle the Telnet service - // If it is started (running, paused, etc), stop the service. // If it is stopped, start the service. ServiceController sc = new ServiceController("Telnet"); Console.WriteLine("The Telnet service status is currently set to {0}", sc.Status.ToString()); if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || (sc.Status.Equals(ServiceControllerStatus.StopPending))) { // Start the service if the current status is stopped. Console.WriteLine("Starting the Telnet service..."); sc.Start(); } else { // Stop the service if its status is not set to "Stopped". Console.WriteLine("Stopping the Telnet service..."); sc.Stop(); } // Refresh and display the current service status. sc.Refresh(); Console.WriteLine("The Telnet service status is now set to {0}.", sc.Status.ToString());
// Toggle the Telnet service - // If it is started (running, paused, etc), stop the service. // If it is stopped, start the service. ServiceController^ sc = gcnew ServiceController( "Telnet" ); if ( sc ) { Console::WriteLine( "The Telnet service status is currently set to {0}", sc->Status ); if ( (sc->Status == (ServiceControllerStatus::Stopped) ) || (sc->Status == (ServiceControllerStatus::StopPending) ) ) { // Start the service if the current status is stopped. Console::WriteLine( "Starting the Telnet service..." ); sc->Start(); } else { // Stop the service if its status is not set to "Stopped". Console::WriteLine( "Stopping the Telnet service..." ); sc->Stop(); } // Refresh and display the current service status. sc->Refresh(); Console::WriteLine( "The Telnet service status is now set to {0}.", sc->Status );
// Toggle the Telnet service - // If it is started (running, paused, etc), stop the service. // If it is stopped, start the service. ServiceController sc = new ServiceController("Telnet"); Console.WriteLine("The Telnet service status is currently set to {0}", sc.get_Status().ToString()); if (sc.get_Status().Equals(ServiceControllerStatus.Stopped) || sc.get_Status().Equals(ServiceControllerStatus.StopPending)) { // Start the service if the current status is stopped. Console.WriteLine("Starting the Telnet service..."); sc.Start(); } else { // Stop the service if its status is not set to "Stopped". Console.WriteLine("Stopping the Telnet service..."); sc.Stop(); } // Refresh and display the current service status. sc.Refresh(); Console.WriteLine("The Telnet service status is now set to {0}.", sc.get_Status().ToString());


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ServiceController コンストラクタ (String, String)
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)

Dim name As String Dim machineName As String Dim instance As New ServiceController(name, machineName)




Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ServiceController コンストラクタ

名前 | 説明 |
---|---|
ServiceController () | 特定のサービスに関連付けられていない ServiceController クラスの新しいインスタンスを初期化します。 |
ServiceController (String) | ローカル コンピュータの既存のサービスに関連付けられている ServiceController クラスの新しいインスタンスを初期化します。 |
ServiceController (String, String) | 指定したコンピュータの既存のサービスに関連付けられている ServiceController クラスの新しいインスタンスを初期化します。 |

ServiceController プロパティ

名前 | 説明 | |
---|---|---|
![]() | CanPauseAndContinue | サービスを一時中断および再開できるかどうかを示す値を取得します。 |
![]() | CanShutdown | システムのシャットダウン時に、サービスにそれを通知する必要があるかどうかを示す値を取得します。 |
![]() | CanStop | サービスをいったん開始してから停止できるかどうかを示す値を取得します。 |
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | DependentServices | この ServiceController インスタンスに関連付けられたサービスに依存している一連のサービスを取得します。 |
![]() | DisplayName | サービスの表示名を取得または設定します。 |
![]() | MachineName | このサービスが常駐しているコンピュータの名前を取得または設定します。 |
![]() | ServiceHandle | サービスのハンドルを取得します。 |
![]() | ServiceName | このインスタンスが参照するサービスを識別する名前を取得または設定します。 |
![]() | ServicesDependedOn | 対象となるサービスが依存している一連のサービス。 |
![]() | ServiceType | 対象となるオブジェクトが参照するサービスの種類を取得します。 |
![]() | Site | Component の ISite を取得または設定します。 ( Component から継承されます。) |
![]() | Status | 対象となるインスタンスが参照するサービスのステータスを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

関連項目
ServiceController クラスSystem.ServiceProcess 名前空間
ServiceBase クラス
ServiceControllerStatus
ServiceType
ServiceController メソッド


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

関連項目
ServiceController クラスSystem.ServiceProcess 名前空間
ServiceBase クラス
ServiceControllerStatus
ServiceType
ServiceController メンバ
Windows サービスを表し、実行中のサービスまたは停止したサービスへの接続、サービスの操作、またはサービスに関する情報の取得を実現します。
ServiceController データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | CanPauseAndContinue | サービスを一時中断および再開できるかどうかを示す値を取得します。 |
![]() | CanShutdown | システムのシャットダウン時に、サービスにそれを通知する必要があるかどうかを示す値を取得します。 |
![]() | CanStop | サービスをいったん開始してから停止できるかどうかを示す値を取得します。 |
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | DependentServices | この ServiceController インスタンスに関連付けられたサービスに依存している一連のサービスを取得します。 |
![]() | DisplayName | サービスの表示名を取得または設定します。 |
![]() | MachineName | このサービスが常駐しているコンピュータの名前を取得または設定します。 |
![]() | ServiceHandle | サービスのハンドルを取得します。 |
![]() | ServiceName | このインスタンスが参照するサービスを識別する名前を取得または設定します。 |
![]() | ServicesDependedOn | 対象となるサービスが依存している一連のサービス。 |
![]() | ServiceType | 対象となるオブジェクトが参照するサービスの種類を取得します。 |
![]() | Site | Component の ISite を取得または設定します。(Component から継承されます。) |
![]() | Status | 対象となるインスタンスが参照するサービスのステータスを取得します。 |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | Dispose | オーバーロードされます。 オーバーライドされます。 |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |


関連項目
ServiceController クラスSystem.ServiceProcess 名前空間
ServiceBase クラス
ServiceControllerStatus
ServiceType
- ServiceControllerのページへのリンク