当前位置: 首页 > news >正文

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());}}
}
http://www.lryc.cn/news/300881.html

相关文章:

  • 【Java程序员面试专栏 分布式中间件】Redis 核心面试指引
  • 2024年【高处安装、维护、拆除】模拟考试题库及高处安装、维护、拆除实操考试视频
  • 【QT+QGIS跨平台编译】之三十七:【Shapelib+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • 【机器学习基础】决策树(Decision Tree)
  • 图神经网络DGL框架,graph classification,多个且不同维度的node feature 训练
  • 蓝桥杯(Web大学组)2022国赛真题:用什么来做计算 A
  • Linux POSIX信号量 线程池
  • Sentinel(理论版)
  • python3 获取某个文件夹所有的pdf文件表格提取表格并一起合并到excel文件
  • 【AIGC】Stable Diffusion的模型入门
  • 【JavaEE】_HTTP请求首行详情
  • Linux第48步_编译正点原子的出厂Linux内核源码
  • 程序员为什么不喜欢关电脑?
  • 【初始RabbitMQ】了解和安装RabbitMQ
  • Linux第56步_根文件系统第3步_将busybox构建的根文件系统烧录到EMMC
  • Linux进程间通信(三)-----System V消息队列
  • Elasticsearch:混合搜索是 GenAI 应用的未来
  • 态、势、感、知的偏序、全序与无序
  • 【从Python基础到深度学习】 8. VIM两种状态
  • java微服务面试篇
  • OpenAI 生成视频模型 Sora 论文翻译
  • 2.13日学习打卡----初学RocketMQ(四)
  • ZigBee学习——BDB
  • 使用Docker快速部署MySQL
  • 力扣热题100_滑动窗口_3_无重复字符的最长子串
  • RM电控工程讲义
  • 论文阅读:《Deep Learning-Based Human Pose Estimation: A Survey》——Part 1:2D HPE
  • C语言——oj刷题——杨氏矩阵
  • C++ 50道面试题
  • 寒假学习记录14:JS字符串