ServiceController.ServiceName プロパティ
アセンブリ: System.ServiceProcess (system.serviceprocess.dll 内)

Dim instance As ServiceController Dim value As String value = instance.ServiceName instance.ServiceName = value
/** @property */ public String get_ServiceName () /** @property */ public void set_ServiceName (String value)
この ServiceController インスタンスが参照するサービスを識別する名前。既定値は空の文字列 ("") です。


サービスは、ServiceName によってサービス コントロール マネージャで識別されます。このプロパティを変更すると、ServiceController インスタンスは別のサービスにバインドします。サービス コントロール マネージャの Microsoft 管理コンソール スナップインの表示は変更されません。
カスタム サービスを実装している場合、このプロパティの値は、対応する ServiceInstaller クラスの ServiceName プロパティのサービスに対して記録された名前と同じ名前にする必要があります。コードでは、ServiceName は、通常、実行可能ファイルの main() 関数に設定されます。
ServiceName プロパティをリセットすると、このプロパティを設定するメソッドによって、このインスタンスの DisplayName は空の文字列 ("") に設定されます。

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.ServiceName プロパティのページへのリンク