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

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

第一种方法

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

csharp 复制代码
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程序

csharp 复制代码
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());
            
        }
       
    }
}
相关推荐
喝醉酒的小白几秒前
如何安装 `.whl` 文件(Python Wheel 包)
开发语言·python
小尤笔记1 分钟前
【2024版】超详细Python+Pycharm安装保姆级教程,Python环境配置和使用指南,看完这一篇就够了
开发语言·ide·python·pycharm·编程语言·解释器
数据大魔方4 分钟前
【期货量化入门】Python获取期货实时行情(TqSdk完整代码)
开发语言·python·区块链
AIFQuant12 分钟前
2026 全球外汇免费实时行情汇率数据 API 接口大全
开发语言·python·websocket·金融·restful
f狐0狸x20 分钟前
【C++修炼之路】C++string的用法
开发语言·c++·string
cjp56031 分钟前
018.C#管道服务,本机两软件间通讯交互
开发语言·c#
猫天意1 小时前
YOLOv11魔改高效涨点 | 注意力篇 | 坐标注意力CoordAttention:将位置信息硬核嵌入通道,精准捕获长程空间依赖,即插即用,涨点神器!!!
开发语言·人工智能·深度学习·神经网络·yolo·目标检测·低光照增强
黎雁·泠崖1 小时前
Java面向对象:this关键字+构造方法+标准JavaBean
java·开发语言·python
码小猿的CPP工坊1 小时前
C++弱引用智能指针std::weak_ptr使用介绍
开发语言·c++
sheji34161 小时前
【开题答辩全过程】以 基于Java的智慧环卫垃圾收运管理系统设计与实现为例,包含答辩的问题和答案
java·开发语言