C#中服务的状态

本文关键字:状态 服务 | 更新日期: 2024-10-20 07:36:19

我正试图从ASP中运行的代码中获取Windows服务的状态。NET网站。这是我的代码:

ServiceController dev1 = new ServiceController();
dev1.MachineName="mac_name";
dev1.ServiceName = "Adobe Acrobat Update Service";
string Dev_Status1 = dev1.Status.ToString();
`

在最后一行中,我得到了一个错误

无法在计算机"mac_name"上打开"adobe acrobat更新服务"InvalidOperationException

C#中服务的状态

"Adobe Acrobat Update Service"是显示名称。服务名称为AdobeARMservice。您可以打开服务控制台Services.msc,然后右键单击该服务并单击"属性"来查看此信息。将同时显示服务名称和显示名称。

请确保您的网站具有访问该计算机的权限。

你的代码应该是。。。

ServiceController sc=new ServiceController("AdobeARMservice", "computername");

如果你想获得许多服务的状态。。。

List<string> MyServicesToCheck=new List<string>() {"AdobeARMservice","2ndservicename","3rdservicename"};
foreach(string ServiceName in MyServicesToCheck)
   {
   ServiceController sc=new ServiceController(ServiceName);
   System.Diagnostics.Debug.WriteLine("Status of "+sc.DisplayName+" is "+sc.Status);
   }