C# 設定程式只能執行一次

C# 設定程式只能執行一次

C# 設定程式只能執行一次

 


資料來源:http://blog.xuite.net/gbisland/csharp/13497681-%E5%A6%82%E4%BD%95%E9%99%90%E5%88%B6%E7%A8%8B%E5%BC%8F%E5%8F%AA%E5%9F%B7%E8%A1%8C%E4%B8%80%E6%AC%A1

http://www.go4expert.com/articles/hiding-windows-c-sharp-t973/


.NET6之前

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

using System.Diagnostics;               // For prcesss related information
using System.Runtime.InteropServices;   // For DLL importing 

namespace CS_Forms_OnlyOne
{
    static class Program
    {
        /*
        SW_HIDE             0
        SW_SHOWNORMAL       1
        SW_NORMAL           1
        SW_SHOWMINIMIZED    2
        SW_SHOWMAXIMIZED    3
        SW_MAXIMIZE         3
        SW_SHOWNOACTIVATE   4
        SW_SHOW             5
        SW_MINIMIZE         6
        SW_SHOWMINNOACTIVE  7
        SW_SHOWNA           8
        SW_RESTORE          9
        SW_SHOWDEFAULT      10
        SW_FORCEMINIMIZE    11
        SW_MAX              11
        */
        private const int SW_HIDE = 0;
        private const int SW_MINIMIZE = 6;
        private const int SW_SHOWNORMAL = 1;
        [DllImport("User32")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);


 
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
			//---
            //判斷應用程式是否己經開啟?
            //http://blog.xuite.net/gbisland/csharp/13497681-%E5%A6%82%E4%BD%95%E9%99%90%E5%88%B6%E7%A8%8B%E5%BC%8F%E5%8F%AA%E5%9F%B7%E8%A1%8C%E4%B8%80%E6%AC%A1
            if((Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1))
            {
                int hWnd;
                MessageBox.Show("應用程式 "+Process.GetCurrentProcess().ProcessName + " 己在執行中,請先關閉舊視窗。Application already execution, please close old form.",Application.ProductName, MessageBoxButtons.OK,MessageBoxIcon.Warning);


 
                //http://www.go4expert.com/articles/hiding-windows-c-sharp-t973/
                Process[] processRunning = Process.GetProcesses();
                foreach (Process pr in processRunning)
                {
                    if (pr.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        hWnd = pr.MainWindowHandle.ToInt32();
                        ShowWindow(hWnd, SW_SHOWNORMAL);
                    }
                }
               
                Application.Exit();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
			//---判斷應用程式是否己經開啟?
			
            Application.Run(new Form1());
        }
    }
}

.NET6

using System.Diagnostics;// For prcesss related information
using System.Runtime.InteropServices;// For DLL importing 

namespace CS_Forms_OnlyOne
{
    internal static class Program
    {
        /*
        SW_HIDE             0
        SW_SHOWNORMAL       1
        SW_NORMAL           1
        SW_SHOWMINIMIZED    2
        SW_SHOWMAXIMIZED    3
        SW_MAXIMIZE         3
        SW_SHOWNOACTIVATE   4
        SW_SHOW             5
        SW_MINIMIZE         6
        SW_SHOWMINNOACTIVE  7
        SW_SHOWNA           8
        SW_RESTORE          9
        SW_SHOWDEFAULT      10
        SW_FORCEMINIMIZE    11
        SW_MAX              11
        */
        private const int SW_HIDE = 0;
        private const int SW_MINIMIZE = 6;
        private const int SW_SHOWNORMAL = 1;
        [DllImport("User32")]
        public static extern int ShowWindow(int hwnd, int nCmdShow);

        [STAThread]
        static void Main()
        {
            //---
            //判斷應用程式是否己經開啟?
            //http://blog.xuite.net/gbisland/csharp/13497681-%E5%A6%82%E4%BD%95%E9%99%90%E5%88%B6%E7%A8%8B%E5%BC%8F%E5%8F%AA%E5%9F%B7%E8%A1%8C%E4%B8%80%E6%AC%A1
            if ((Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1))
            {
                int hWnd;
                MessageBox.Show("應用程式 " + Process.GetCurrentProcess().ProcessName + " 己在執行中,請先關閉舊視窗。Application already execution, please close old form.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);



                //http://www.go4expert.com/articles/hiding-windows-c-sharp-t973/
                Process[] processRunning = Process.GetProcesses();
                foreach (Process pr in processRunning)
                {
                    if (pr.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        hWnd = pr.MainWindowHandle.ToInt32();
                        ShowWindow(hWnd, SW_SHOWNORMAL);
                    }
                }

                Application.Exit();
                return;
            }
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //---判斷應用程式是否己經開啟?

            ApplicationConfiguration.Initialize();
            Application.Run(new Form1());
        }
    }
}


One thought on “C# 設定程式只能執行一次

  1. C# Console BAT 命令模式 程式 只能執行一次

    //---
    //程式重複執行防呆機制
    //http://blog.xuite.net/gbisland/csharp/13497681-%E5%A6%82%E4%BD%95%E9%99%90%E5%88%B6%E7%A8%8B%E5%BC%8F%E5%8F%AA%E5%9F%B7%E8%A1%8C%E4%B8%80%E6%AC%A1
    if ((Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1))
    {
    //http://www.go4expert.com/articles/hiding-windows-c-sharp-t973/
    Process[] processRunning = Process.GetProcesses();
    foreach (Process pr in processRunning)
    {
    if (pr.ProcessName == Process.GetCurrentProcess().ProcessName)
    {
    ShowWindow(pr.MainWindowHandle, SW_SHOWNORMAL);
    }
    }

    Environment.Exit(0);
    return;
    }
    //---程式重複執行防呆機制

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *