Example #1
0
 private void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     _timer.Stop();
     AutoUpdater.Start(AutoUpdater.AppTitle);
 }
        private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs asyncCompletedEventArgs)
        {
            if (asyncCompletedEventArgs.Cancelled)
            {
                return;
            }

            if (asyncCompletedEventArgs.Error != null)
            {
                if (!AutoUpdater.IsSilentMode)
                {
                    MessageBox.Show(asyncCompletedEventArgs.Error.Message,
                                    asyncCompletedEventArgs.Error.GetType().ToString(), MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                AutoUpdater.LogMessage($"{this.GetType()}", "WebClientOnDownloadFileCompleted",
                                       asyncCompletedEventArgs.Error);

                _webClient = null;
                Close();

                return;
            }

            if (!string.IsNullOrEmpty(AutoUpdater.Checksum))
            {
                if (!CompareChecksum(_tempFile, AutoUpdater.Checksum))
                {
                    _webClient = null;
                    Close();
                    return;
                }
            }

            ContentDisposition contentDisposition = null;

            if (_webClient.ResponseHeaders["Content-Disposition"] != null)
            {
                contentDisposition = new ContentDisposition(_webClient.ResponseHeaders["Content-Disposition"]);
            }

            var fileName = string.IsNullOrEmpty(contentDisposition?.FileName)
                ? Path.GetFileName(_webClient.ResponseUri.LocalPath)
                : contentDisposition.FileName;

            var tempPath =
                Path.Combine(
                    string.IsNullOrEmpty(AutoUpdater.DownloadPath) ? Path.GetTempPath() : AutoUpdater.DownloadPath,
                    fileName);

            try
            {
                if (File.Exists(tempPath))
                {
                    File.Delete(tempPath);
                }

                File.Move(_tempFile, tempPath);
            }
            catch (Exception e)
            {
                if (!AutoUpdater.IsSilentMode)
                {
                    MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                AutoUpdater.LogMessage($"{this.GetType()}", "WebClientOnDownloadFileCompleted", e);
                _webClient = null;
                Close();
                return;
            }

            AutoUpdater.InstallerArgs = AutoUpdater.InstallerArgs.Replace("%path%",
                                                                          Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName));

            var processStartInfo = new ProcessStartInfo
            {
                FileName        = tempPath,
                UseShellExecute = true,
                Arguments       = AutoUpdater.InstallerArgs
            };

            var extension = Path.GetExtension(tempPath);

            if (extension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
            {
                string installerPath = Path.Combine(Path.GetDirectoryName(tempPath), "ZipExtractor.exe");

                try
                {
                    File.WriteAllBytes(installerPath, Resources.ZipExtractor);
                }
                catch (Exception e)
                {
                    if (!AutoUpdater.IsSilentMode)
                    {
                        MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    AutoUpdater.LogMessage($"{this.GetType()}", "WebClientOnDownloadFileCompleted", e);
                    _webClient = null;
                    Close();
                    return;
                }

                StringBuilder arguments =
                    new StringBuilder($"\"{tempPath}\" \"{Process.GetCurrentProcess().MainModule.FileName}\"");
                string[] args = Environment.GetCommandLineArgs();
                for (int i = 1; i < args.Length; i++)
                {
                    if (i.Equals(1))
                    {
                        arguments.Append(" \"");
                    }

                    arguments.Append(args[i]);
                    arguments.Append(i.Equals(args.Length - 1) ? "\"" : " ");
                }

                processStartInfo = new ProcessStartInfo
                {
                    FileName        = installerPath,
                    UseShellExecute = true,
                    Arguments       = arguments.ToString()
                };
            }
            else if (extension.Equals(".msi", StringComparison.OrdinalIgnoreCase))
            {
                processStartInfo = new ProcessStartInfo
                {
                    FileName  = "msiexec",
                    Arguments = $"/i \"{tempPath}\""
                };
                if (!string.IsNullOrEmpty(AutoUpdater.InstallerArgs))
                {
                    processStartInfo.Arguments += " " + AutoUpdater.InstallerArgs;
                }
            }

            if (AutoUpdater.RunUpdateAsAdmin)
            {
                processStartInfo.Verb = "runas";
            }

            try
            {
                Process.Start(processStartInfo);
            }
            catch (Win32Exception exception)
            {
                _webClient = null;
                AutoUpdater.LogMessage($"{this.GetType()}", "WebClientOnDownloadFileCompleted", exception);
                if (exception.NativeErrorCode != 1223)
                {
                    throw;
                }
            }

            Close();
        }