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());
            
        }
       
    }
}
相关推荐
我真的不会C7 分钟前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio7 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
火柴盒zhang13 分钟前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
景天科技苑20 分钟前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
阿让啊26 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
椰羊~王小美31 分钟前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言
孞㐑¥1 小时前
C++11介绍
开发语言·c++·经验分享·笔记
旦莫1 小时前
Python 教程:我们可以给 Python 文件起中文名吗?
开发语言·python
꧁坚持很酷꧂2 小时前
配置Ubuntu18.04中的Qt Creator为中文(图文详解)
开发语言·qt·ubuntu
不当菜虚困2 小时前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式