代码如下:
注意:先要添加引用System.ServiceProcess
class Program
{
static void Main(string[] args)
{
Console.WriteLine(ServiceIsExisted("MSSQLSERVER"));
}
public static bool ServiceIsExisted(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
return true;
}
}
return false;
}
}