LoginSignup
1
1

More than 1 year has passed since last update.

Windowsサービスの実行状態を取得/実行

Last updated at Posted at 2022-02-03

Windowsサービスの状態を取得したり動かしたり

using System.ServiceProcess;

// Print Spooler = サービス名
using (ServiceController sc = new ServiceController("Print Spooler"))
{
  //プロパティ値を更新
  sc.Refresh();

  if (sc.Status == ServiceControllerStatus.Stopped)
  {
    // サービスが止まってたら動かす
    sc.Start();
    // サービスが動くまで最大1分間待つ
    sc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 1, 0));
  }
}
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1