[C#]EXE執行檔單獨執行以及已經執行時做還原

  • 12873
  • 0
  • 2010-08-02

EXE執行檔的問題,可以分成兩個部份: 1. EXE執行檔已經執行時,就不在開啟新的程式出來;2. 如果EXE執行檔是最小化的時候,將它顯示在最前面

在藍色小舖時遇到這個問題,主要是要問EXE執行檔的問題,可以分成兩個部份
1. EXE執行檔已經執行時,就不在開啟新的程式出來
2. 如果EXE執行檔是最小化的時候,將它顯示在最前面
 
會用到這三個相關的命名空間
1. System.Diagnostics
   http://msdn.microsoft.com/zh-tw/library/system.diagnostics(VS.80).aspx
2. System.Runtime.InteropServices
   http://msdn.microsoft.com/zh-tw/library/system.runtime.interopservices.aspx
3. System.Reflection
   http://msdn.microsoft.com/zh-tw/library/system.reflection(VS.80).aspx

相關的使用方式已經註解在程式碼內,程式碼如下

using System;
using System.Collections.Generic;
using System.Windows.Forms;

using System.Diagnostics;   // 允許您與系統處理序 (Process)、事件記錄檔和效能計數器互動
using System.Runtime.InteropServices;   // 提供各種支援 COM Interop 和平台叫用服務的成員
using System.Reflection; // 提供已載入型別、方法和欄位的 Managed 檢視,並具有動態建立和叫用 (Invoke) 型別的功能

namespace WinTest
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Get the running instance.    
            Process instance = RunningInstance();
            if (instance == null)
            {
                System.Windows.Forms.Application.EnableVisualStyles();   // 這兩行實現XP可視風格    
                System.Windows.Forms.Application.DoEvents();
                // There isn't another instance, show our form (Main Form).    
                Application.Run(new Main());
            }

            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; // 1.Normal   2.Minimized   3.Maximized
    }

}

參考
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090213164530GII&fumcde=
http://studio.zeuik.com/wordpress/?p=80

檔案下載

WinTest.rar