c# 限制同时启动多个实例程序运行


 

 

 
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();

            }

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM