日期:2014-05-19  浏览次数:20734 次

只知道该应用程序的.exe可执行文件名,如何判断该应用程序是否已经运行?
譬如有一个myApp.exe,   请问如何判断该应用程序是否已经运行
虽然可以通过Process.GetProcessByName( "processName ")查找进程来获得,但是myApp和processName之间有什么联系,会一样吗?
现在只知道应用程序名,怎么才能知道该应用程序运行后的进程名呢?
可以通过注册表查找吗?可是也不知道myApp应用程序具体的安装路径的
望高手解答。。。

------解决方案--------------------
是一样,遍历进程是可以的!
------解决方案--------------------
foreach (Process oth in Process.GetProcesses())
{
if (oth.ProcessName.ToUpper() == "QQ ")
{
this.richTextBox1.AppendText(oth.ProcessName + "\n ");
this.richTextBox1.AppendText(oth.Id.ToString()+ "\n ");
}
}
------解决方案--------------------
用lianshaohua的方法就可以了,c#有Process类为什么要去调用API呢?
------解决方案--------------------
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;

其他代码省略。仅仅从一个form启动讲。每个form都有个main入口。
static void Main()
{ Process instance = RunningInstance(); //获取运行中的实例
if (instance==null)
Application.Run(new Form1());
else
{
HandleRunningInstance(instance);//处理得到的进程。
}
}
private static Process RunningInstance()
{Process current = Process.GetCurrentProcess(); //得到当前form1的进程
Process[] processes = Process.GetProcessesByName(current.ProcessName);// 得到所有同名进程。
foreach(Process MyProcess in processes)
{
if(MyProcess.Id!= current.Id) //不同的进程id,也就是肯定不是同一进程
{
if(Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\\ ")==current.MainModule.FileName) //进程名不同而id不同 的时候,比较程序启动的路径
return MyProcess;

}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show( "程序已经运行! ");
ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。


}
[DllImport( "User32.dll ")]
private static externbool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport( "User32.dll ")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);

------解决方案--------------------
在myApp中定义一个静态变量,当有实例运行时就把此静态变量设置为true。
要判断是否已经有实例在运行时,只要判断其静态变量即可。
------解决方案--------------------
Process.MainModule.FileName可以得到进程对应的文件名