[C#]EXE執行檔單獨運行小技巧

[C#]EXE執行檔單獨運行小技巧

這個問題是最近討論區朋友問到的問題,把它記錄在我的Blog裡,如下:

EXE執行檔已經運行了,如果我再去運行同樣EXE檔時,新的程序不再運行,
而是將原來的EXE執行檔顯示在最前面,如果EXE執行檔是最小化的時候,先將它最大化後再顯示在最前面,
outlook2003,迅雷、金山詞霸2009就是這樣的效果。

網路上有一個很好的解法,如下(C#):

01 using System;
02 using System.Collections.Generic;
03 using System.Windows.Forms;
04 using System.Diagnostics;
05 using System.Runtime.InteropServices;
06 using System.Reflection;
07   
08 namespace WindowsApplication3
09 {
10     static class Program
11     {
12         [STAThread]
13         static void Main()
14         {
15             //Get   the   running   instance.   
16             Process instance = RunningInstance();
17             if (instance == null)
18             {
19                 System.Windows.Forms.Application.EnableVisualStyles();
20                 System.Windows.Forms.Application.DoEvents();
21   
22                 //There  isn't another instance, show our form.   
23                 Application.Run(new Form1());
24             }
25             else
26             {
27                 //There is another instance of this process.  
28                 HandleRunningInstance(instance);
29             }
30         }
31   
32         public static Process RunningInstance()
33         {
34             Process current = Process.GetCurrentProcess();
35             Process[] processes = Process.GetProcessesByName(current.ProcessName);
36   
37             //Loop through  the running processes in with the same name   
38             foreach (Process process in processes)
39             {
40                 //Ignore   the   current   process  
41                 if (process.Id != current.Id)
42                 {
43                     //Make sure that the process is running from the exe file.   
44                     if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
45                     {
46                         //Return   the   other   process   instance.  
47                         return process;
48                     }
49                 }
50             }
51             //No other instance was found, return null. 
52             return null;
53         }
54   
55         public static void HandleRunningInstance(Process instance)
56         {
57             //Make sure the window is not minimized or maximized 
58             ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
59             //Set the real intance to foreground window
60             SetForegroundWindow(instance.MainWindowHandle);
61         }
62   
63         [DllImport("User32.dll")]
64         private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
65         [DllImport("User32.dll")]
66         private static extern bool SetForegroundWindow(IntPtr hWnd);
67   
68         //1:normal
69         //2:minimized
70         //3:maximized
71         private const int WS_SHOWNORMAL = 3;
72     }
73 }

 

參考網址:
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090213164530GII&fumcde=FUM20050124192253INM#BRD20090215180413HRO
http://www.cnblogs.com/ghostljj/archive/2007/10/02/912979.html

posted @ 2010-08-27 09:37  peterlee  阅读(319)  评论(0)    收藏  举报