在C#中,如何查询Windows服务器上正在运行的服务列表? [英] In C# how do i query the list of running services on a windows server?
本文介绍了在C#中,如何查询Windows服务器上正在运行的服务列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查询作为特定用户在远程计算机上运行的服务列表,然后检查每个服务的运行状况。我正在构建一个自定义控制台。
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.
推荐答案
要使用ServiceController方法,我将使用在此实现模拟的解决方案上一个问题: 。Net 2.0 ServiceController.GetServices()
To use the ServiceController method I'd check out the solution with impersonation implemented in this previous question: .Net 2.0 ServiceController.GetServices()
FWIW,这是带有显式主机,用户名和密码的C#/ WMI方法:
FWIW, here's C#/WMI way with explicit host, username, password:
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));
}
}
这篇关于在C#中,如何查询Windows服务器上正在运行的服务列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文