Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type or namespace name 'ServiceController' could not be found

Tags:

c#

.net

windows

I am trying to check a service in C#, I have added the System.ServiceProcess.dll

Although I get the error:

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

My code is as follows:

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

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

Do I perhaps need a "using" statement?

like image 726
Mike Avatar asked Jun 04 '11 17:06

Mike


2 Answers

You need to add a reference to the System.ServiceProcess.dll

enter image description here

After that, you will be able to see it in Visual Studio, as one of the using statements you can add to your project:

enter image description here

like image 142
Gustavo Mori Avatar answered Oct 03 '22 21:10

Gustavo Mori


Pro Tip: When you are trying to use a class from the .NET Framework and you get a message like:

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

Lookup the type in the MSDN Library and look under the Inheritance Hierarchy section to find the Namespace and Assembly you need.

Inheritance Hierarchy

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

Namespace: System.ServiceProcess
Assembly: System.ServiceProcess (in System.ServiceProcess.dll)

Then ensure that you have a reference to the assembly and a using directive for the namespace (assuming you don't want to fully qualify the name).

like image 23
Robert Groves Avatar answered Oct 03 '22 22:10

Robert Groves