如何检测系统是否已经运行了相同的程序。

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

using System;

using System.Xml;

using System.ComponentModel ;

using System.Windows.Forms;

using System.Diagnostics;

using System.Runtime;

using System.Reflection;

using System.Runtime.InteropServices;

[STAThread]

static void Main() //主程序运行

{

Process intance=RunningIntance(); //调用检查函数

if(intance==null) //不存在相同的程序

{

Application.Run(new frmMain());

}

else //存在相同的程序

{

HandleRunningInstance(intance);

}

}

private static Process RunningIntance()

{

Process currentProcess=Process.GetCurrentProcess();

Process [] processCollection=Process.GetProcessesByName(currentProcess.ProcessName);

foreach(Process p in processCollection)

{

if(p.Id!=currentProcess.Id) //检查ID是否相同

{

//检查运行文件路径是否相同

if(Assembly.GetExecutingAssembly().Location.Replace("/","\\")==currentProcess.MainModule.FileName)

return currentProcess;

}

}

return null;

}

private static void HandleRunningInstance(Process instance)

{

MessageBox.Show("该应用系统(PSP)已经在运行!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);

ShowWindowAsync(instance.MainWindowHandle,1); //调用api函数,正常显示窗口

SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。

}

[DllImport("User32.dll")]

private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);

[DllImport("User32.dll")]

private static extern bool SetForegroundWindow(System.IntPtr hWnd);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航