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

use*_*147 18 .net c# service assembly-references

这是代码.我只是想测试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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

什么地方出了错?

Son*_*nül 47

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

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

在此输入图像描述

这个程序集可能在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727文件夹中.


小智 10

安装 NuGet 包 System.ServiceProcess.ServiceController

  • 已确认适用于 .Net 6.0 及更高版本 (3认同)

wal*_*her 5

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

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