找不到类型或命名空间名称"ServiceController"

Mik*_*ike 12 .net c# windows

我正在尝试用C#检查服务,我已添加了 System.ServiceProcess.dll

虽然我收到错误:

错误2找不到类型或命名空间名称'ServiceController'(您是否缺少using指令或程序集引用?)D:\ App\Form1.cs 247 13 App

我的代码如下:

private void button13_Click(object sender, EventArgs e)
{
    ServiceController sc = new ServiceController("Spooler");

    if (sc.Status == ServiceControllerStatus.Running)
    {
        MessageBox.Show("The service is running.");
    }
}
Run Code Online (Sandbox Code Playgroud)

我是否需要"使用"声明?

Gus*_*ori 15

您需要添加对System.ServiceProcess.dll的引用

在此输入图像描述

之后,您将能够在Visual Studio中看到它,作为using可以添加到项目中的语句之一:

在此输入图像描述


Rob*_*ves 5

专业提示:当您尝试使用.NET Framework中的类时,您会收到如下消息:

找不到类型或命名空间名称"..."(您是否缺少using指令或程序集引用?)

MSDN Library中查找类型,并在"继承层次结构"部分下查找所需的命名空间和程序集.

继承层次结构

System.Object   
  System.MarshalByRefObject  
    System.ComponentModel.Component  
      System.ServiceProcess.ServiceController  
Run Code Online (Sandbox Code Playgroud)

命名空间: System.ServiceProcess
程序集: System.ServiceProcess(在System.ServiceProcess.dll中)

然后确保您具有对程序集的引用和命名空间的using指令(假设您不想完全限定名称).