天天看点

[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);
    }
             

复制