如何检查当前正在运行的控制台程序? [英] How to check a console program currently running or not ?

查看:135
本文介绍了如何检查当前正在运行的控制台程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我刚刚在C#中创建了两个控制台程序,分别名为console1和console2.现在,当我运行console1时,必须检查console2是否正在运行吗?请告诉我代码.
谢谢

Hi,

I just created two console programs in c# named console1 and console2.Now,when i run the console1 it have to check whether the console2 is currently running or not?Kindly tell me the code.

Thanks

推荐答案

使用以下方法
use the following method
private bool IsAlreadyRunning(string ExecutableFileName)
{
return Process.GetProcesses().ToList().Where(obj=>obj.ProcessName==ExecutableFileName).ToList().Count==0?false:true;
}



如果您想找到console1,只需致电



if you want to find console1 simply call

bool bac=IsAlreadyRunning("console1");


使用Process类:
Use the Process class:
private static bool IsRunning(string name)
    {
    Process[] processes = Process.GetProcesses();

    foreach (Process p in processes)
        {
        if (p.ProcessName == name)
            {
            return true;
            }
        }
    return false;
    }


这篇关于如何检查当前正在运行的控制台程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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