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

26

这是代码。我只是想测试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”的内容(是否缺少程序集引用?)

出了什么问题?


1
你是否缺失一个程序集引用?错误信息已经指向了解决方案。查看 ServiceController ,你会看到这个:程序集: System.ServiceProcess (in System.ServiceProcess.dll) - Remus Rusanu
4个回答

64

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

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

enter image description here

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


9

安装NuGet包System.ServiceProcess.ServiceController


3
已确认适用于 .Net 6.0 及以上版本。 - Tidy

6

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

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


3

你应该从框架列表中添加此内容。

右键单击项目 -> 添加引用 -> 在 "程序集" 下搜索 -> 选择 -> 确定。

输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接