2013-11-04 79 views
14

這是代碼。我只想測試System.ServiceProcess庫的庫。爲什麼我的C#沒有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類型或命名空間名稱ServiceProcess「不 命名空間「系統」存在(是缺少程序組裝參考?)

出了什麼問題?

+1

'你缺少程序集引用? 「錯誤已經引導你走向解決方案。查找['ServiceController'](http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller(v = vs.110).aspx),你會看到:'Assembly:System .ServiceProcess(在System.ServiceProcess.dll中)' –

回答

35

System.ServiceProcess命名空間是屬於上System.ServiceProcess.dll,它不添加爲默認的參考。

爲此,在解決方案窗口中,右鍵單擊「References」並選擇「Add Reference ..」轉到.NET選項卡,然後雙擊System.ServiceProcess.dll

enter image description here

該組件可能是C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727文件夾中。

4

您還需要添加對相應.dll的引用。

右鍵單擊項目 - >添加引用 - >組件 - >框架 - > System.ServiceProcess

2

,你應該從框架列表 添加此右擊項目 - >添加引用 - 在「組件」>搜索 - >選擇 - > OK

enter image description here