C#では、Windowsサーバー上の実行中のサービスのリストをどのように照会しますか?
リモートマシン上の特定のユーザーとして実行されているサービスのリストを照会し、それぞれの健康を確認したいと思います。私はカスタムコンソールを構築しています。
ServiceControllerメソッドを使用するには、この前の質問に実装されている偽装を使用してソリューションをチェックアウトします。 .Net 2.0 ServiceController.GetServices()
FWIW、明示的なホスト、ユーザー名、パスワードを持つC#/ WMIの方法は次のとおりです。
using System.Management;
static void EnumServices(string host, string username, string password)
{
string ns = @”rootcimv2″;
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));
}
}
source