2011-06-04 131 views
9

我想您在C#中的服务,我已经添加了System.ServiceProcess.dll类型或命名空间名称“的ServiceController”找不到

虽然我得到的错误:

Error 2 The type or namespace name 'ServiceController' could not be found (are you missing a using directive or an assembly reference?) 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."); 
    } 
} 

我是否需要一个“使用”语句?

回答

14

您需要添加一个引用到System.ServiceProcess.dll

enter image description here

之后,你就可以看到它在Visual Studio中,为using之一报表可以添加到您的项目:

enter image description here

+0

感谢您向我们展示如何找到它。 – DOK 2011-06-04 17:37:13

+0

@DOK我的荣幸! – 2011-06-04 17:38:33

3

是的,在顶部。

​​

+0

干杯,现在就像一个魅力! – Mike 2011-06-04 17:36:03

5

专业提示:当你试图用一个类从.NET框架,你会得到这样的消息:在MSDN Library

The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)

查找类型和下继承层次结构部分,找到命名空间看和大会你需要。

继承层次

System.Object 
    System.MarshalByRefObject 
    System.ComponentModel.Component 
     System.ServiceProcess.ServiceController 

命名空间:System.ServiceProcess
大会: System.ServiceProcess(在System.ServiceProcess.dll

然后保证你有a reference to the assemblyusing directive为命名空间(假设您不想完全限定名称)。

+0

谢谢,这对未来会很方便 – Mike 2011-06-04 17:48:16

1

对我(的Visual Studio 2012),它在打字时,“使用” 我不得不添加引用,并通过组件的System.ServiceProcess搜索是不是默认添加。 (我相信它位于旧版Studio中的.NET选项卡中)。 希望这有助于任何未来的观众!

+0

这与答案答案完全相同,除非没有支持图表。 – newfurniturey 2012-10-05 14:33:39

相关问题