Exemplo n.º 1
0
        private static void ShowNewUpdateAvailableForm(Tuple <VersionNumber, ProductVersionDto, bool> versionInfo)
        {
            VersionNumber     currentVersion       = versionInfo.Item1;
            ProductVersionDto latestProductVersion = versionInfo.Item2;
            bool skipNewVersion = versionInfo.Item3;

            bool DoNotShowAgain = FormProductUpdate.DialogShow(currentVersion, latestProductVersion, skipNewVersion);


            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(Const.PRODUCT_REG_KEY, true);

            if (registryKey == null)
            {
                registryKey = Registry.CurrentUser.CreateSubKey(Const.PRODUCT_REG_KEY);
            }

            if (registryKey != null)
            {
                string currentValue =
                    registryKey.GetValue(Const.PRODUCT_REG_KEY_VALUE_DONT_SHOW_UPDATE_VERSION) as string;
                List <VersionNumber> versionsToSkip = string.IsNullOrEmpty(currentValue)
                                                         ? new List <VersionNumber>()
                                                         : ParseVersionsToSkip(currentValue);

                VersionNumber latestVersion = new VersionNumber(latestProductVersion.Version);

                if (DoNotShowAgain)
                {
                    if (!versionsToSkip.Any(item => item.Equals(latestVersion)))
                    {
                        versionsToSkip.Add(latestVersion);
                    }
                }
                else
                {
                    versionsToSkip.RemoveAll(number => number.Equals(latestVersion));
                }

                currentValue = string.Join(";", versionsToSkip.Select(item => item.ToString()).ToArray());

                registryKey.SetValue(Const.PRODUCT_REG_KEY_VALUE_DONT_SHOW_UPDATE_VERSION, currentValue);
                registryKey.Close();
            }
        }
Exemplo n.º 2
0
        private static void InternalCheckForUpdate(VersionNumber currentVersion, bool showInfoWhenNoUpgrade)
        {
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(Const.PRODUCT_REG_KEY);
            string      dontShowUpdateForVersion = null;

            if (registryKey != null)
            {
                dontShowUpdateForVersion =
                    registryKey.GetValue(Const.PRODUCT_REG_KEY_VALUE_DONT_SHOW_UPDATE_VERSION) as string;
                registryKey.Close();
            }

            Action showErrorAction = () => MessageBox.Show("Error getting information about new version, please try again later.", "Error",
                                                           MessageBoxButtons.OK,
                                                           MessageBoxIcon.Error);

            synchronizationContext = SynchronizationContext.Current;

            WebClient client = new WebClient();
            Uri       uri    = new Uri(string.Format(CHECK_VERSION_URL_FORMAT, SERVER_FOR_UPDATES, currentVersion, debugMode ? "debug" : "!"));

            client.DownloadStringCompleted += delegate(object sender, DownloadStringCompletedEventArgs args)
            {
                try
                {
                    if (!args.Cancelled && args.Error == null)
                    {
                        bool hasNewVersionInfo = !string.IsNullOrEmpty(args.Result);

                        try
                        {
                            ProductVersionDto latestProductVersion = hasNewVersionInfo ? ProductVersionDto.FromXml(args.Result) : null;
                            if (latestProductVersion != null)
                            {
                                VersionNumber latestProductVersionId = new VersionNumber(latestProductVersion.Version);
                                if (latestProductVersionId.CompareTo(currentVersion) == 1)
                                {
                                    var versionsToSkip = ParseVersionsToSkip(dontShowUpdateForVersion);

                                    bool skipNewVersion     = versionsToSkip.Any(latestProductVersionId.Equals);
                                    bool canShowUpgradeForm = versionsToSkip.Count == 0 || !skipNewVersion;

                                    if (canShowUpgradeForm || showInfoWhenNoUpgrade)
                                    {
                                        synchronizationContext.Post(
                                            state =>
                                            ShowNewUpdateAvailableForm((Tuple <VersionNumber, ProductVersionDto, bool>)state),
                                            new Tuple <VersionNumber, ProductVersionDto, bool>(currentVersion,
                                                                                               latestProductVersion, skipNewVersion));
                                    }
                                }
                            }
                            else
                            {
                                if (showInfoWhenNoUpgrade)
                                {
                                    ProductVersionDto dummpyProductVersion = new ProductVersionDto(currentVersion.ToString());
                                    FormProductUpdate.DialogShow(currentVersion, dummpyProductVersion, false);
                                }
                            }
                        }
                        catch
                        {
                            showErrorAction();
                        }
                    }
                    else
                    {
                        showErrorAction();
                    }
                }
                finally
                {
                    alreadyExecuted = false;
                }
            };
            client.DownloadStringAsync(uri);
        }