Windows Service Status

Service

Das Microsoft .Net Framework bietet eine einfache Möglichkeit festzustellen, ob ein bestimmter Windows Dienst gestartet ist und läuft.

C# Beispielcode:

(Verweis auf System.ServiceProcess muss vorab eingefügt werden)

using System;
using System.ServiceProcess;

namespace IsServiceRunning
{
  public class Program
  {
    public static void Main(string[] args)
    {
      const string serviceName = "Windows Search";
      Console.WriteLine("Service '{0}' running: {1}", serviceName, IsRunning(serviceName).ToString());
      Console.ReadLine();
    }

    /// <summary>
    /// windows service running ?
    /// </summary>
    /// <param name="serviceName">service name</param>
    /// <returns>true or false</returns>
    private static bool IsRunning(string serviceName)
    {
      try
      {
        ServiceController sc = new ServiceController(serviceName);
        return sc.Status == ServiceControllerStatus.Running;
      }
      catch
      {
        return false;
      }
    }
  }
}

Entsprechenden VB-Code findet man hier: http://dotnet-snippets.de/snippet/pruefen-ob-ein-dienst-gestartet-ist/157

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s