2011-06-04 117 views
9

我想您在C#中的服務,我已經添加了System.ServiceProcess.dll類型或命名空間名稱「的ServiceController」找不到

雖然我得到的錯誤:

Error 2 The type or namespace name 'ServiceController' could not be found (are you missing a using directive or an assembly reference?) D:\App\Form1.cs 247 13 App

我的代碼如下:

private void button13_Click(object sender, EventArgs e) 
{ 
    ServiceController sc = new ServiceController("Spooler"); 

    if (sc.Status == ServiceControllerStatus.Running) 
    { 
     MessageBox.Show("The service is running."); 
    } 
} 

我是否需要一個「使用」語句?

回答

14

您需要添加一個引用到System.ServiceProcess.dll

enter image description here

之後,你就可以看到它在Visual Studio中,爲using之一報表可以添加到您的項目:

enter image description here

+0

感謝您向我們展示如何找到它。 – DOK 2011-06-04 17:37:13

+0

@DOK我的榮幸! – 2011-06-04 17:38:33

3

是的,在頂部。

​​

+0

乾杯,現在就像一個魅力! – Mike 2011-06-04 17:36:03

5

專業提示:當你試圖用一個類從.NET框架,你會得到這樣的消息:在MSDN Library

The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)

查找類型和下繼承層次結構部分,找到命名空間看和大會你需要。

繼承層次

System.Object 
    System.MarshalByRefObject 
    System.ComponentModel.Component 
     System.ServiceProcess.ServiceController 

命名空間:System.ServiceProcess
大會: System.ServiceProcess(在System.ServiceProcess.dll

然後保證你有a reference to the assemblyusing directive爲命名空間(假設您不想完全限定名稱)。

+0

謝謝,這對未來會很方便 – Mike 2011-06-04 17:48:16

1

對我(的Visual Studio 2012),它在打字時,「使用」 我不得不添加引用,並通過組件的System.ServiceProcess搜索是不是默認添加。 (我相信它位於舊版Studio中的.NET選項卡中)。 希望這有助於任何未來的觀衆!

+0

這與答案答案完全相同,除非沒有支持圖表。 – newfurniturey 2012-10-05 14:33:39

相關問題