检测系统是否已经运行了相同的程序
2004-11-03 01:56
549 查看
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);
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);
相关文章推荐
- ATL实现Connection Point的一种简单的方法
- oracle中的分页
- 獲取SQL SERVER用戶表信息游標
- 力争最简单把*、&说清楚
- 一个画图类,类似ms_chart的线图,适用于科学仿真数据分析(一)
- 怎样为任何控件和区域添加提示信息:用自己封装的CTip类
- 从注册表中还原MSNMessenger口令
- 2004/09/17又重装系统了——知道vsconfig.xml是谁生成的吗?
- 从数据库中动态选取下拉列表的方法
- 如何在VB例程中接收自定义消息
- UML应用点滴积累
- Tomcat中通过JNDI配置访问数据库
- Mysql中左连接的使用
- 移居csdn,今天想到自己的写shell的能力需要提高~
- SymbianOS Series 60学习笔记三:数据库初探
- [转贴]绕过xp sp2防火墙的代码
- creating a Self Deleting Executable
- Phrack最新公布的内核态RootKit的技术细节(e文)