在 C# 中,如何查询 Windows 服务器上正在运行的服务列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C# how do i query the list of running services on a windows server?
提问by kkelly18
I want to query for a list of services running as a specific user on a remote machine and then check the health of each. I'm building a custom console.
我想查询在远程机器上以特定用户身份运行的服务列表,然后检查每个服务的运行状况。我正在构建一个自定义控制台。
采纳答案by xcud
To use the ServiceController method I'd check out the solution with impersonation implemented in this previous question: .Net 2.0 ServiceController.GetServices()
要使用 ServiceController 方法,我将查看在上一个问题中实现的模拟解决方案: .Net 2.0 ServiceController.GetServices()
FWIW, here's C#/WMI way with explicit host, username, password:
FWIW,这是带有显式主机、用户名、密码的 C#/WMI 方式:
using System.Management;
static void EnumServices(string host, string username, string password)
{
string ns = @"root\cimv2";
string query = "select * from Win32_Service";
ConnectionOptions options = new ConnectionOptions();
if (!string.IsNullOrEmpty(username))
{
options.Username = username;
options.Password = password;
}
ManagementScope scope =
new ManagementScope(string.Format(@"\{0}\{1}", host, ns), options);
scope.Connect();
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, new ObjectQuery(query));
ManagementObjectCollection retObjectCollection = searcher.Get();
foreach (ManagementObject mo in retObjectCollection)
{
Console.WriteLine(mo.GetText(TextFormat.Mof));
}
}
回答by Shea
You can use wmi for this (System.Management). You can also use ServiceController.GetServices()
.
您可以为此使用 wmi (System.Management)。您也可以使用ServiceController.GetServices()
.
回答by Aaron Daniels
ServiceController.GetServices("machineName")
returns an array of ServiceController
objects for a particular machine.
ServiceController.GetServices("machineName")
返回ServiceController
特定机器的对象数组。
This:
这个:
namespace AtYourService
{
using System;
using System.ServiceProcess;
class Program
{
static void Main(string[] args)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
Console.WriteLine(
"The {0} service is currently {1}.",
service.DisplayName,
service.Status);
}
Console.Read();
}
}
}
produces:
产生:
The Application Experience service is currently Running.
The Andrea ST Filters Service service is currently Running.
The Application Layer Gateway Service service is currently Stopped.
The Application Information service is currently Running.
etc...
Of course, I used the parameterless version to get the services on my machine.
当然,我使用无参数版本在我的机器上获取服务。
回答by Mehedi hasan
This will check you system's service name with your desired service name which you can mention on parameter
这将使用您可以在参数中提及的所需服务名称检查您的系统服务名称
namespace ServiceName
{
using System;
using System.ServiceProcess;
class Service
{
public static bool IsServiceInstalled(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName == serviceName)
return true;
}
return false;
}
}
}