예제 #1
0
 /// <summary>
 ///     Is calles when the request for information about a available update has been completed.
 /// </summary>
 /// <param name="sender">The WebClient that completed the operation.</param>
 /// <param name="e">
 ///     <see cref="UploadStringCompletedEventArgs" />
 /// </param>
 private void UpdateUploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
 {
     try
     {
         if (e.Error != null)
         {
             var args = new UpdateErrorEventArgs($"Could not reach the update server..");
             this.ErrorOccured?.Invoke(this, args);
         }
         else
         {
             UpdateResponse response = JsonConvert.DeserializeObject <UpdateResponse>(e.Result);
             this.downloadLink = response.Link;
             if (!string.IsNullOrEmpty(this.downloadLink))
             {
                 this.AvailableUpdateDetected?.Invoke(this, new EventArgs());
             }
         }
     }
     catch (JsonReaderException)
     {
         this.ErrorOccured?.Invoke(
             this,
             new UpdateErrorEventArgs($"Could not parse the data that was returned from the update server."));
     }
 }
예제 #2
0
        /// <summary>
        ///     Is called when download of a file has completed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            // Extracts the update.zip file to the directory that the launcher assembly is in.
            new ZipExtractor().Extract("update.zip", Path.GetDirectoryName(this.GetType().Assembly.Location));

            // The launcher assembly has to be replaces(requires a restart).
            if (File.Exists("G2O_Launcher.exe.update"))
            {
                try
                {
                    StringBuilder batch = new StringBuilder();
                    batch.AppendLine($"taskkill /F /IM {Assembly.GetExecutingAssembly().GetName().Name}.exe");
                    string appPath = Assembly.GetExecutingAssembly().Location;

                    batch.AppendLine("timeout /t 1 /nobreak");
                    batch.AppendLine($"move /y \"{appPath}.update\" \"{appPath}\"");
                    batch.AppendLine($"\"{appPath}\"");
                    batch.AppendLine("del /F \"%~f0\"");
                    batch.AppendLine("exit");

                    string batchPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".bat");
                    File.WriteAllText(batchPath, batch.ToString());

                    ProcessStartInfo processStart = new ProcessStartInfo();
                    processStart.WindowStyle            = ProcessWindowStyle.Hidden;
                    processStart.FileName               = batchPath;
                    processStart.UseShellExecute        = true;
                    processStart.RedirectStandardOutput = false;
                    processStart.RedirectStandardError  = false;
                    Process.Start(processStart);

                    //// Kill the G2O_Update process if it already exists.
                    // foreach (var process in Process.GetProcessesByName("G2O_Update.exe"))
                    // {
                    // process.Kill();
                    // }

                    // if (Process.Start("G2O_Update.exe") != null)
                    // {
                    // Environment.Exit(0);
                    // }
                    // else
                    // {
                    // var args = new UpdateErrorEventArgs($"Cannot start update process!");
                    // this.ErrorOccured?.Invoke(this, args);
                    // }
                }
                catch (Win32Exception)
                {
                    var args = new UpdateErrorEventArgs($"G2O_Update.exe didn't exist!");
                    this.ErrorOccured?.Invoke(this, args);
                }
            }

            // Update complete no restart required.
            else
            {
                this.UpdateCompleted?.Invoke(this, new EventArgs());
            }
        }