检查过程是用C#在远程系统上运行 [英] Check if a process is running on a remote system using C#

查看:140
本文介绍了检查过程是用C#在远程系统上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个过程,是一个远程系统上运行。我现在用的是下面的code:

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

然而,当我尝试运行code,我得到以下错误:无法连接到远程计算机

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

我可以用下面的命令来运行,则PsList: C:>则PsList \ remoteSystemName 所以,我知道这是可能得到我需要的信息,但我需要它在code。

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.

另一种可能性是PsList将整合到C#和搜索列表来查看该过程是存在的,但我还没有找到如何做到这一点的信息。

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.

推荐答案

使用了 System.ServiceProcess.ServiceController 类的服务。您可以使用状态来检查它是否运行和停止()开始() 来控制它。

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();
}

这篇关于检查过程是用C#在远程系统上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆