2014-09-02 19 views
-2

我创建了一个窗口服务,并将其用于我的类中。我只想检查是否无法找到类型或名称空间'ServiceController'

服务正在运行或没有运行。为此,我刚刚添加了参考

System.ServiceProcess.dll在我的项目中,并尝试下面的代码。

System.ServiceProcess.ServiceController sc = new ServiceController(SERVICENAME); 

但它给错误:

System.ServiceProcess.ServiceController sc = new ServiceController(SERVICENAME); 

这样的:类型或命名空间“的ServiceController”找不到

+0

可能重复[类型或命名空间名称'ServiceController'找不到](http://stackoverflow.com/questions/6238295/the-type-or-namespace-name-servicecontroller-could-not-be-found) – atur 2014-09-02 10:19:48

+0

我hav Ë看到该线程,但它不能解决这个问题。 – user2750155 2014-09-02 10:22:05

+0

是否添加了'using System.ServiceProcess;'你的班级? – Kooki 2014-09-02 10:26:36

回答

1

我也有这个问题,不断变化的解决了这个问题:

var sc = new System.ServiceProcess.ServiceController(SERVICENAME); 
+0

我在引用中包含了System.ServiceProcess ..但是当我添加..时我得到错误。在顶部使用Sysetm.ServiceProcess。可能是什么问题。 – user2750155 2014-09-02 10:49:42

+0

然后在下一个答案中使用完整版CodeCaster。如果你真的添加了正确的引用,你必须使用'使用System.ServiceProcess' – Kooki 2014-09-02 10:52:22

0

只读错误,看看th e您的IDE中红线的确切位置。

你有

System.ServiceProcess.ServiceController sc = 
    new ServiceController(SERVICENAME); 

第二次你不使用名称空间,显然你缺少一个using声明。

您可以通过两种方式解决这个问题:

使用完整的命名空间:

System.ServiceProcess.ServiceController sc = 
    new System.ServiceProcess.ServiceController(SERVICENAME); 

或者在你的文件的开头添加using声明:中

using System.ServiceProcess; 

// ... 

ServiceController sc = new ServiceController(SERVICENAME); 
相关问题