为什么我的C#没有System.ServiceProcess库?

这是代码。 我只是想测试System.ServiceProcess库的库。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceProcess; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("hi"); var srv = new ServiceController("MyService"); Console.WriteLine("MyService Status {0}", srv.Status); if (srv.Status != ServiceControllerStatus.Running) srv.Start(); System.Threading.Thread.Sleep(1000000); } } } 

但是,当我运行C#代码时,它说:

错误1命名空间“System”中不存在类型或命名空间名称“ServiceProcess”(您是否缺少程序集引用?)

什么地方出了错?

System.ServiceProcess命名空间属于System.ServiceProcess.dll ,默认情况下不会添加为引用。

为此,在解决方案窗口中,右键单击“引用”并选择“添加引用”。转到.NET选项卡,然后双击System.ServiceProcess.dll

在此处输入图像描述

此程序集可能位于C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727文件夹中。

您还需要添加对相应.dll的引用。

右键单击项目 – >添加引用 – >程序集 – >框架 – > System.ServiceProcess

你应该从框架列表中添加它右键单击项目 – >添加参考 – >搜索“Assemblies” – > select-> OK

在此处输入图像描述