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