private static bool CompareChecksum(string fileName, string checksum)
        {
            using (var hashAlgorithm = HashAlgorithm.Create(AutoUpdater.HashingAlgorithm))
            {
                using (var stream = File.OpenRead(fileName))
                {
                    if (hashAlgorithm != null)
                    {
                        var hash         = hashAlgorithm.ComputeHash(stream);
                        var fileChecksum = BitConverter.ToString(hash).Replace("-", String.Empty).ToLowerInvariant();

                        if (fileChecksum == checksum.ToLower())
                        {
                            return(true);
                        }
                        if (!AutoUpdater.IsSilentMode)
                        {
                            MessageBox.Show(Resources.FileIntegrityCheckFailedMessage,
                                            Resources.FileIntegrityCheckFailedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        AutoUpdater.LogMessage(LogMessageEventArgs.Type.Error, $"DownloadUpdateDialog", "CompareChecksum",
                                               Resources.FileIntegrityCheckFailedMessage);
                    }
                    else
                    {
                        if (AutoUpdater.ReportErrors)
                        {
                            if (!AutoUpdater.IsSilentMode)
                            {
                                MessageBox.Show(Resources.HashAlgorithmNotSupportedMessage,
                                                Resources.HashAlgorithmNotSupportedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            AutoUpdater.LogMessage(LogMessageEventArgs.Type.Error, $"DownloadUpdateDialog", "CompareChecksum",
                                                   Resources.HashAlgorithmNotSupportedMessage);
                        }
                    }

                    return(false);
                }
            }
        }
        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();
        }