c# 限制同时启动多个实例程序运行
2021-07-09 04:07
标签:orm mes har win nbsp 实例 getname rop div c# 限制同时启动多个实例程序运行 标签:orm mes har win nbsp 实例 getname rop div 原文地址:https://www.cnblogs.com/wgscd/p/9718622.html
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public class MultiRunChecker{
public static bool hasMultiRun()
{
Process[] ps = Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name);
if (ps != null && ps.Length > 1)
{
//发现重复进程
return true;
}
return false;
}
}
}
调用:
//防止改名运行
if (!Application.ExecutablePath.EndsWith(Assembly.GetExecutingAssembly().GetName().Name+".exe")) {
MessageBox.Show("app not valid");
Application.Exit();
}
if (MultiRunChecker.hasMultiRun()) {、、防止多个程序运行
MessageBox.Show("has");
Application.Exit();
}