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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19763527/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:48:59  来源:igfitidea点击:

Why my C# does not have System.ServiceProcess Library?

c#.netserviceassembly-references

提问by user1535147

This is the code. I just wanna test the library of System.ServiceProcess library.

这是代码。我只是想测试 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);
        }
    }
}

However, when I run the C# code, its says:

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

Error 1 The type or namespace name 'ServiceProcess' does not exist in the namespace 'System' (are you missing an assembly reference?)

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

What went wrong?

什么地方出了错?

采纳答案by Soner G?nül

System.ServiceProcessnamespace belongs on System.ServiceProcess.dlland it doesn't added as a reference by default.

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

For this, in the solution window, right click on "References" and choose "Add Reference.." Go to the .NET tab, and double click on System.ServiceProcess.dll.

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

enter image description here

在此处输入图片说明

This assembly is probably in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727folder.

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

回答by walther

You need to add a reference to the corresponding .dll as well.

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

Right click on the project -> Add Reference -> Assemblies -> Framework -> System.ServiceProcess

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

回答by reza.cse08

you should add this from framework list Right click on the project -> Add Reference -> search under "Assemblies" -> select->OK

您应该从框架列表中添加它右键单击项目-> 添加引用-> 在“程序集”下搜索-> 选择-> 确定

enter image description here

在此处输入图片说明