示例#1
0
        private static void RaiseCheckForUpdates(UpdateInfoEventArgs args)
        {
            var handler = CheckForUpdateEvent;

            if (handler != null)
            {
                handler(args);
            }
        }
示例#2
0
        private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            var mainAssembly = Assembly.GetEntryAssembly();

            var companyAttribute = GetAttribute <AssemblyCompanyAttribute>(mainAssembly);
            var titleAttribute   = GetAttribute <AssemblyTitleAttribute>(mainAssembly);

            AppTitle         = titleAttribute != null ? titleAttribute.Title : mainAssembly.GetName().Name;
            InstalledVersion = mainAssembly.GetName().Version;

            var appCompany = companyAttribute != null ? companyAttribute.Company : "";

            RegistryLocation = string.IsNullOrEmpty(appCompany)
                ? string.Format(@"Software\{0}\AutoUpdater", AppTitle)
                : string.Format(@"Software\{0}\{1}\AutoUpdater", appCompany, AppTitle);

            var updateKey = Registry.CurrentUser.OpenSubKey(RegistryLocation);

            if (updateKey != null)
            {
                var remindLaterTime = updateKey.GetValue("remindlater");
                if (remindLaterTime != null)
                {
                    var remindLater = Convert.ToDateTime(
                        remindLaterTime.ToString(),
                        CultureInfo.CreateSpecificCulture("en-US"));

                    var compareResult = DateTime.Compare(DateTime.Now, remindLater);

                    if (compareResult < 0)
                    {
                        var mainWindow = new MainWindow(true);
                        mainWindow.SetTimer(remindLater);
                        return;
                    }
                }
            }

            var webRequest = WebRequest.Create(AppCastUrl);

            webRequest.CachePolicy = new HttpRequestCachePolicy(
                HttpRequestCacheLevel.NoCacheNoStore);

            WebResponse webResponse;

            try
            {
                webResponse = webRequest.GetResponse();
            }
            catch (Exception)
            {
                RaiseCheckForUpdates(null);
                return;
            }

            var receivedAppCastDocument = new XmlDocument();
            var appCastStream           = webResponse.GetResponseStream();

            if (appCastStream != null)
            {
                receivedAppCastDocument.Load(appCastStream);
            }
            else
            {
                RaiseCheckForUpdates(null);
                return;
            }

            ReadAppCastItems(receivedAppCastDocument, webResponse);

            if (updateKey != null)
            {
                var skip = updateKey.GetValue("skip");
                var applicationVersion = updateKey.GetValue("version");
                if (skip != null && applicationVersion != null)
                {
                    var skipValue   = skip.ToString();
                    var skipVersion = new Version(applicationVersion.ToString());
                    if (skipValue.Equals("1") && CurrentVersion <= skipVersion)
                    {
                        return;
                    }

                    if (CurrentVersion > skipVersion)
                    {
                        var updateKeyWrite = Registry.CurrentUser.CreateSubKey(RegistryLocation);
                        if (updateKeyWrite != null)
                        {
                            updateKeyWrite.SetValue("version", CurrentVersion.ToString());
                            updateKeyWrite.SetValue("skip", 0);
                        }
                    }
                }
                updateKey.Close();
            }

            if (CurrentVersion == null)
            {
                return;
            }

            var args = new UpdateInfoEventArgs
            {
                DownloadUrl       = DownloadUrl,
                ChangelogUrl      = ChangeLogUrl,
                CurrentVersion    = CurrentVersion,
                InstalledVersion  = InstalledVersion,
                IsUpdateAvailable = false
            };

            if (CurrentVersion > InstalledVersion)
            {
                args.IsUpdateAvailable = true;

                if (CheckForUpdateEvent == null)
                {
                    ShowUi();
                }
            }

            RaiseCheckForUpdates(args);
        }