Привет, сегодня я покажу тебе код, который не позволит пользователю открыть приложение второй раз. Собственно код в двух строках: var mutex = new Mutex(false, "97c91428a4034fe2a220cla44a95ef2b", out var cretedNew); if (!cretedNew) { Environment.Exit(0); } Код var mutex = new Mutex(false, "97c91428a4034fe2a220cla44a95ef2b", out var cretedNew); if (!cretedNew) { Environment.Exit(0); }
Лучше будет так: public static string GetGUID() { string result; try { Assembly assembly = typeof(Program).Assembly; var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; result = attribute.Value; } catch { result = "CF2D4313-33DE-489D-9721-6AFF69841DEY"; } return result?.ToUpper(); } public static bool Inizialize() { var app = new Mutex(true, GetGUID(), out bool isNew); GC.KeepAlive(app); return isNew; } Код public static string GetGUID() { string result; try { Assembly assembly = typeof(Program).Assembly; var attribute = (GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]; result = attribute.Value; } catch { result = "CF2D4313-33DE-489D-9721-6AFF69841DEY"; } return result?.ToUpper(); } public static bool Inizialize() { var app = new Mutex(true, GetGUID(), out bool isNew); GC.KeepAlive(app); return isNew; }