C# – Check if a process is running on a remote system using C#

.netc++system.diagnostics

I am trying to check if a process is running on a remote system. I am using the following code:

string procSearc = "notepad";
string remoteSystem = "remoteSystemName";

Process[] proce = System.Diagnostics.Process.GetProcessesByName(procSearch, remoteSystem);

However, when I try to run the code, I get the following error: "Couldn't connect to remote machine."

I am able to run pslist with the following command:
C:>pslist \remoteSystemName
So I know it is possible to get the information I need, but I need it in the code.

Another possibility would be to integrate pslist into C# and search the list to see if the process is there, but I have not found information on how to do this.

Best Solution

Use the System.ServiceProcess.ServiceController class for a service. You can use Status to check if it's running and the Stop() and Start() to control it.

ServiceController sc = new ServiceController();
sc.MachineName = remoteSystem;
sc.ServiceName = procSearc;

if (sc.Status.Equals(ServiceControllerStatus.Running))
{
   sc.Stop();
}
else
{
   sc.Start();
}