using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
namespace ServiceStatusTest {
class Program {
static void Main(string[] args) {
try {
bool result = false;
ServiceControllerStatus status = GetServiceStatus("Service1", ".");
if (status == ServiceControllerStatus.Running) {
status = GetServiceStatus("Service2", ".");
if (status == ServiceControllerStatus.Running) {
result = true;
}
}
if (result == true) {
Console.WriteLine("Services are running");
} else {
Console.WriteLine("Services are not running.");
}
} catch (Exception ex) {
Console.Write(ex.ToString());
}
}
///
/// Return the status of a specific service
///
/// Name of the service to start
/// Machine running the service
/// Status of the specified services
/// Thrown when a bad service name or machine is provided
public static ServiceControllerStatus GetServiceStatus(String name, String machine) {
using (ServiceController controller = new ServiceController(name, machine)) {
return controller.Status;
}
}
}
}