在C#中,如何查询Windows服务器上正在运行的服务列表?

编程入门 行业动态 更新时间:2024-10-28 11:22:30
本文介绍了在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服务器上正在运行的服务列表?

本文发布于:2023-08-07 06:41:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正在运行   器上   列表   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!