Philogogus
Philogogus
CC#
Created by Philogogus on 10/6/2023 in #help
❔ Returning a bool value when searching for the status of a service running on a remote server
Howdy, I am trying to return the status of a remote service. I can get it to work fine with something like Print Spooler but when I try to return the status of the service I actually WANT... it doesn't work.
public static bool IsServiceRunning(string serviceName, string remoteMachineName)
{
ServiceController sc = new ServiceController(serviceName, remoteMachineName);

try
{
ServiceControllerStatus status = sc.Status;
return (status == ServiceControllerStatus.Running);
}
catch (InvalidOperationException)
{

return false;
}
}
public static bool IsServiceRunning(string serviceName, string remoteMachineName)
{
ServiceController sc = new ServiceController(serviceName, remoteMachineName);

try
{
ServiceControllerStatus status = sc.Status;
return (status == ServiceControllerStatus.Running);
}
catch (InvalidOperationException)
{

return false;
}
}
Got this off of StackOverflow, and it seems to work because when I check for Print Spooler it returns YES.
try
{
bool serverup = IsServiceRunning(".BLARG Server", "TESTSERVER-T06");

if (serverup)
{
this.EventLog.WriteEntry("It's up!");
}
else
{
this.EventLog.WriteEntry("It's down!");
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.ToString());
}
try
{
bool serverup = IsServiceRunning(".BLARG Server", "TESTSERVER-T06");

if (serverup)
{
this.EventLog.WriteEntry("It's up!");
}
else
{
this.EventLog.WriteEntry("It's down!");
}
}
catch (Exception ex)
{
this.EventLog.WriteEntry(ex.ToString());
}
5 replies