このインスタンスが参照するサービスを識別する名前を取得または設定します。
Public Property ServiceName As String
[C#]
public string ServiceName {get; set;}
[C++]
public: __property String* get_ServiceName();public: __property void set_ServiceName(String*);
[JScript]
public function get ServiceName() : String;public function set ServiceName(String);
プロパティ値
この ServiceController インスタンスが参照するサービスを識別する名前。既定値は空の文字列 ("") です。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | ServiceName が null 参照 (Visual Basic では Nothing) です。 |
ArgumentException | ServiceName プロパティの構文が無効です。 |
解説
サービスは、 ServiceName によってサービス コントロール マネージャで識別されます。このプロパティを変更すると、 ServiceController インスタンスは別のサービスにバインドします。サービス コントロール マネージャの Microsoft 管理コンソール スナップインの表示は変更されません。
カスタム サービスを実装している場合、このプロパティの値は、対応する ServiceInstaller クラスの ServiceName プロパティのサービスに対して記録された名前と同じ名前にする必要があります。コードでは、 ServiceName は、通常、実行可能ファイルの main()
関数に設定されます。
ServiceName プロパティをリセットすると、このプロパティを設定するメソッドによって、このインスタンスの DisplayName は空の文字列 ("") に設定されます。
使用例
[Visual Basic, C#, C++] 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
[C#]
// 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.");
}
}
[C++]
// Check whether the Alerter service is started.
ServiceController *sc = new ServiceController();
if (sc)
{
sc->ServiceName = "Alerter";
Console::WriteLine("The Alerter service status is currently set to {0}",
__box(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}.",
__box(sc->Status));
}
catch (InvalidOperationException *e)
{
Console::WriteLine("Could not start the Alerter service.");
}
}
}
[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 名前空間