如何在C#中检查exe是否在命令提示符下运行 [英] How to check if an exe is running in command prompt in C#

查看:128
本文介绍了如何在C#中检查exe是否在命令提示符下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我需要一段代码来检查一个或多个exe实例是否正在运行。如果是这样,它不应该允许运行。如果我直接运行我的应用程序,它提供了适当的消息,但如果我运行我的命令提示符,它不会。如果它在命令提示符下运行,如何停止它。请帮忙。



我尝试过:



Hello,
I need a piece of code that checks whether one or more instance of exe is running or not. If so, it should not allow to run. If I run my application directly, it provides me appropriate message but whereas if I run my command prompt it doesn't. How to stop it if it is running in command prompt. Please help.

What I have tried:

if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
                {
                   Console.WriteLine("App is already running on this computer. Only one instance of App can run at a time");
                    return;
                }

推荐答案

参见仅运行一个实例c - Google搜索 [ ^ ]。


试试这个:

Try this:
Process thisProcess = Process.GetCurrentProcess();
if (Process.GetProcessesByName(thisProcess.ProcessName).Count() > 1)
    {
    Console.WriteLine("Allready running");
    }


查看 c# - 确保只有一个应用程序实例 - Stack Overflow [ ^ ]& 将C#应用程序限制为单个实例 [ ^ ]
Look at c# - Ensuring only one application instance - Stack Overflow[^] & Restricting a C# Application to a Single Instance[^]


这篇关于如何在C#中检查exe是否在命令提示符下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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