| 发表于:2007-03-09 09:50:464楼 得分:0 |
using system; using system.runtime.interopservices; using system.windows.forms; using system.diagnostics; using system.reflection; public class oneinstnace { [stathread] public static void main() { //get the running instance. process instance = runninginstance(); if (instance == null) { //there isn 't another instance, show our form. application.run (new form()); } else { //there is another instance of this process. handlerunninginstance(instance); } } public static process runninginstance() { process current = process.getcurrentprocess(); process[] processes = process.getprocessesbyname (current.processname); //loop through the running processes in with the same name foreach (process process in processes) { //ignore the current process if (process.id != current.id) { //make sure that the process is running from the exe file. if (assembly.getEXECutingassembly().location.replace( "/ ", "\\ ") == current.mainmodule.filename) { //return the other process instance. return process; } } } //no other instance was found, return null. return null; } public static void handlerunninginstance(process instance) { //make sure the window is not minimized or maximized showwindowasync (instance.mainwindowhandle , ws_shownormal); //set the real intance to foreground window setforegroundwindow (instance.mainwindowhandle); } [dllimport( "user32.dll ")] private static extern bool showwindowasync( intptr hwnd, int cmdshow); [dllimport( "user32.dll ")] private static extern bool setforegroundwindow(intptr hwnd); private const int ws_shownormal = 1; } | | |
|