Mike
Mike

Reputation: 249

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

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?

Upvotes: 12

Views: 23408

Answers (4)

wjhguitarman
wjhguitarman

Reputation: 1073

For me (Visual Studio 2012) it was not a default addition when typing in "using" I had to add a reference and search through the assemblies for System.ServiceProcess. (I believe it is located in the .NET tab in older versions of Studio). Hope this helps any future viewers!

Upvotes: 1

Robert Groves
Robert Groves

Reputation: 7748

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).

Upvotes: 5

Gustavo Mori
Gustavo Mori

Reputation: 8386

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

Upvotes: 16

ataddeini
ataddeini

Reputation: 4951

Yes, at the top.

using System.ServiceProcess;

Upvotes: 3

Related Questions