C# 只允许开启一个exe程序

C# 只允许开启一个exe程序

第一种方法

电脑只能启动一次再次点击显示当前exe程序

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BulletinBoard
{
   
    static class Program
    {
   
        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        public static extern int SetForegroundWindow(IntPtr hwnd);
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x112;
        public const int SC_RESTORE = 0xF120;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
   
             Process cur = Process.GetCurrentProcess();
             foreach (Process p in Process.GetProcesses())
             {
   
                 if (p.Id == cur.Id) continue;
                 if (p.ProcessName == cur.ProcessName)
                 {
   
                     SetForegroundWindow(p.MainWindowHandle);
                     SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_RESTORE, 0);
                     return;
                 }
             }
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             Application.Run(new Form1());
            
        }
    }
}

第二种方法

电脑只能启动一次再次点击重新启动exe程序

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BulletinBoard
{
   
    static class Program
    {
   
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
   
        
            Process current = Process.GetCurrentProcess();
    Process[] pp = Process.GetProcessesByName(current.ProcessName);
    foreach(Process p in pp)
    {
   
        if (p.Id != current.Id)
        {
   
            if (p.MainModule.FileName == current.MainModule.FileName)
            {
   
                p.Kill();
                break;
            }
        }
     }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            
        }
       
    }
}

相关推荐

  1. C# 允许开启一个exe程序

    2024-02-19 00:52:02       50 阅读
  2. MFC:允许产生一个应用程序实例的具体实现

    2024-02-19 00:52:02       24 阅读
  3. C# 程序启动另外一个exe的时候传参数

    2024-02-19 00:52:02       35 阅读
  4. 【QT】QSharedMemory 打包后exe运行一个实例

    2024-02-19 00:52:02       32 阅读
  5. input允许输入数字

    2024-02-19 00:52:02       30 阅读
  6. mysql怎么允许指定IP访问

    2024-02-19 00:52:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 00:52:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 00:52:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 00:52:02       87 阅读
  4. Python语言-面向对象

    2024-02-19 00:52:02       96 阅读

热门阅读

  1. [C++] 分支优化

    2024-02-19 00:52:02       49 阅读
  2. Leetcode-1523. 在区间范围内统计奇数数目

    2024-02-19 00:52:02       50 阅读
  3. 顺子日期 蓝桥杯

    2024-02-19 00:52:02       48 阅读
  4. 【orbslam2+nerf】

    2024-02-19 00:52:02       52 阅读
  5. Python 键盘模拟

    2024-02-19 00:52:02       52 阅读
  6. 24 双非计算机秋招总结

    2024-02-19 00:52:02       50 阅读
  7. 数据库事务的 4 种隔离级别

    2024-02-19 00:52:02       47 阅读
  8. C Primer Plus(第六版)16.17 复习题 第6题

    2024-02-19 00:52:02       50 阅读
  9. 110 C++ decltype含义,decltype 主要用途

    2024-02-19 00:52:02       41 阅读
  10. python - 文件

    2024-02-19 00:52:02       59 阅读
  11. C++练习

    C++练习

    2024-02-19 00:52:02      48 阅读