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?
You need to add a reference to the System.ServiceProcess.dll
After that, you will be able to see it in Visual Studio, as one of the using
statements you can add to your project:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With