'ServiceController' 不包含 'GetServices' 的定义

'ServiceController' does not contain a definition for 'GetServices'

我用一个C#形式的APP列出我电脑上的所有服务。 但是当我按照所有答案列出所有服务时,我得到:

'ServiceController' does not contain a definition for 'GetServices'

我所有的研究导致:

using ServiceStack.Host;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace myFromApp
{
    public partial class Services : Form
    {
        public Services()
        {
            InitializeComponent();
        }

 private void RefreshServiceBbutton_Click(object sender, EventArgs e)
        {
            ServiceController[] services = ServiceController.GetServices();
           foreach (ServiceController scTemp in scServices)
{
   if (scTemp.Status == ServiceControllerStatus.Running)
   {
      //do stuff with each service(display info and such)
  }
}

   }

   }
}

`

您必须导入命名空间 System.ServiceProcess 然后您可以调用 ServiceController[] services = ServiceController.GetServices(); 获取服务列表。