天天看點

[C#]限制程式隻能運作一個執行個體 (防多開)

[C#]限制程式隻能運作一個執行個體 (防多開)
Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); // no ".exe"
    if (processes.Length > 1)
    {
      log.Fatal("Another instance is running.");
      Environment.Exit(1);
    }
               

複制

如果想自動關閉視窗,隻需要更改為:

Process[] processes = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName); // no ".exe"
    if (processes.Length > 1)
    {
      log.Fatal("Another instance is running.");
      Environment.Exit(0);
    }
             

複制