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());
            
        }
       
    }
}
相关推荐
lin zaixi()31 分钟前
埃筛C++写法
开发语言·c++
Yasen.M1 小时前
流体中的流线【StreamLines】的实现
开发语言·python·manim·manim动画·python动画·数学演示
Qzer_4071 小时前
JVM(Java Virtual Machine,Java虚拟机)
java·开发语言·jvm
눈明空1 小时前
Java超市收银系统(十、爬虫)
java·开发语言·数据库·爬虫·mysql·eclipse·idea
墨尔本、晴2 小时前
[C语言]一、C语言基础(函数)
c语言·开发语言
倔强的石头1062 小时前
【C++指南】类和对象(一):类和对象的定义和使用 基础讲解
开发语言·c++
陈大爷(有低保)2 小时前
Java面向接口编程——开发打印机
java·开发语言
m0_619731192 小时前
USB详解,配置及难点
开发语言·stm32·单片机·嵌入式硬件
9毫米的幻想2 小时前
【C++】—— 内存管理
c语言·开发语言·jvm·c++·学习
funnycoffee1232 小时前
excel vba将选中区域向下复制指定次数
java·开发语言·excel