Esempio n. 1
0
        private static void UpdateExecutableFile()
        {
            while (PreloaderWindow.CurrentWindow == null)
            {
                Thread.Sleep(100);
            }

            try
            {
                PreloaderWindow.ShowFourthStep();
                Process[] processesList  = Process.GetProcesses();
                Process   currentProcess = Process.GetCurrentProcess();

                List <Process> otherProcess = processesList.Where(p => p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id).ToList();
                foreach (Process process in otherProcess.Where(p => !p.HasExited))
                {
                    process.Kill();
                }

                string currentExecutingPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string currentExeFileName   = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
                if (string.IsNullOrEmpty(currentExecutingPath) || string.IsNullOrEmpty(currentExeFileName))
                {
                    throw new Exception("Failed to get path or name of current executing assembly.");
                }

                string mainAssemblyPath = Path.GetDirectoryName(currentExecutingPath);
                if (string.IsNullOrEmpty(mainAssemblyPath))
                {
                    throw new Exception("Failed to get path to main application.");
                }

                FileSystemHelper.CopyDirectoryRecursively(currentExecutingPath, mainAssemblyPath);
                string mainExeFilePath = Path.Combine(mainAssemblyPath, currentExeFileName);
                if (string.IsNullOrEmpty(mainExeFilePath) || !File.Exists(mainExeFilePath))
                {
                    throw new Exception("Failed to find temporary folder deleting .EXE file.");
                }

                string commandLine = "/deletetemp /left=" + PreloaderWindow.GetLeftPosition() + " /top=" + PreloaderWindow.GetTopPosition();
                Process.Start(mainExeFilePath, commandLine);
                Process.GetCurrentProcess().Kill();
            }
            catch (Exception e)
            {
                MessageBox.Show("Failed to update executable file. " + e.Message, "Updating error", MessageBoxButton.OK, MessageBoxImage.Error);
                Process.GetCurrentProcess().Kill();
            }
        }