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());
            
        }
       
    }
}
相关推荐
ShineWinsu10 分钟前
对于Qt:QMediaPlayer和QVideoWidget的解析
开发语言·c++·qt·视频·客户端·qmediaplayer·媒体播放器
开开心心_Every14 分钟前
安卓手写文字生成工具,多种纸张一直免费
服务器·开发语言·jupyter·智能手机·计算机外设·vim·php
打工仔折腾 AI28 分钟前
C++ AI SDK(十):DataManager的数据库读写、事务与一致性设计
开发语言·数据库·c++·人工智能·后端·python·性能优化
benchmark_cc38 分钟前
vn.py 如何接入 A 股历史行情?使用 QuantDash 映射 BarData 并导入回测数据库
开发语言·数据库·python·pandas·量化交易·股票数据·quantdash
BK贝壳1 小时前
copy() 出来的副本改了会不会牵动本体?bool-hybrid-array 深拷贝本机实测,对照 view() 视图共享
开发语言·数据结构·python·性能优化·numpy
hudd6681 小时前
JavaEE 初阶 - 网络编程(第二部分:网络编程套接字)
java·开发语言·网络·java-ee
妮妮分享1 小时前
定位坐标偏移几百米?GCJ-02 / WGS84 / BD-09 坐标系踩坑实录(JS 附代码)
开发语言·javascript·坐标系转换·wgs84·gcj02
sharemylove1 小时前
Java 继承与权限修饰符 学习笔记
java·开发语言·笔记·学习·课程设计
老秦包你会1 小时前
计算机网络第五课 ----应⽤层⾃定义协议与序列化(包含守护进程)
开发语言·计算机网络
青山木2 小时前
JVM 内存区域全解:堆、栈、方法区、直接内存的职责划分与常见报错
java·开发语言·jvm·后端·内存·堆·方法区