windows 找不到类型或命名空间名称“ServiceController”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6238295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type or namespace name 'ServiceController' could not be found
提问by Mike
I am trying to check a service in C#, I have added the System.ServiceProcess.dll
我正在尝试在 C# 中检查服务,我添加了 System.ServiceProcess.dll
Although I get the error:
虽然我收到错误:
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
错误 2 找不到类型或命名空间名称“ServiceController”(您是否缺少 using 指令或程序集引用?)D:\App\Form1.cs 247 13 App
My code is as follows:
我的代码如下:
private void button13_Click(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("Spooler");
if (sc.Status == ServiceControllerStatus.Running)
{
MessageBox.Show("The service is running.");
}
}
Do I perhaps need a "using" statement?
我可能需要“使用”声明吗?
回答by Gustavo Mori
You need to add a reference to the System.ServiceProcess.dll
您需要添加对 System.ServiceProcess.dll 的引用
After that, you will be able to see it in Visual Studio, as one of the using
statements you can add to your project:
之后,您将能够在 Visual Studio 中看到它,作为using
您可以添加到项目中的语句之一:
回答by Robert Groves
Pro Tip: When you are trying to use a class from the .NET Framework and you get a message like:
专业提示:当您尝试使用 .NET Framework 中的类时,您会收到如下消息:
The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)
找不到类型或命名空间名称“...”(您是否缺少 using 指令或程序集引用?)
Lookup the type in the MSDN Libraryand look under the Inheritance Hierarchy section to find the Namespace and Assembly you need.
在MSDN 库中查找类型并在继承层次结构部分下查找您需要的命名空间和程序集。
Inheritance Hierarchy
继承层次结构
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.ServiceProcess.ServiceController
Namespace:System.ServiceProcess
Assembly:System.ServiceProcess (in System.ServiceProcess.dll)
命名空间:System.ServiceProcess
程序集:System.ServiceProcess(在System.ServiceProcess.dll 中)
Then ensure that you have a reference to the assemblyand a using directivefor the namespace (assuming you don't want to fully qualify the name).
回答by ataddeini
Yes, at the top.
是的,在顶部。
using System.ServiceProcess;
using System.ServiceProcess;
回答by wjhguitarman
For me (Visual Studio 2012) it was not a default addition when typing in "using" I had to add a reference and search through the assemblies for System.ServiceProcess. (I believe it is located in the .NET tab in older versions of Studio). Hope this helps any future viewers!
对我(Visual Studio 2012)来说,在输入“使用”时它不是默认添加,我必须添加引用并搜索 System.ServiceProcess 的程序集。(我相信它位于旧版 Studio 的 .NET 选项卡中)。希望这可以帮助任何未来的观众!