currently trying to write a C# program which can detect a specific status of a service. If the service is running, the program will close it. If the service is stopped, the program will run it.
Below is my code:
ServiceController sc = new ServiceController("RtkAudioService");
Console.WriteLine("Status = " + sc.Status);
Console.WriteLine("Can Pause and Continue = " + sc.CanPauseAndContinue);
Console.WriteLine("Can ShutDown = " + sc.CanShutdown);
Console.WriteLine("Can Stop = " + sc.CanStop);
Console.WriteLine("Machine Name = " + sc.MachineName);
if (sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
}
else if (sc.Status.Equals(ServiceControllerStatus.Running))
{
sc.Stop();
}
I have tried to stop/run various service for my pc but failed. The RtkAudioService is just one of my example that failed. All of the error message are the same:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceProcess.dll
Additional information: Cannot open RtkAudioService service on computer '.'.
Do you guys know what's happening?