示例#1
0
        private static bool StartUpdate(object result)
        {
            if (result is DateTime time)
            {
                SetTimer(time);
            }
            else
            {
                if (result is UpdateInfoEventArgs args)
                {
                    CheckForUpdateEvent?.Invoke(args);

                    if (args.IsUpdateAvailable)
                    {
                        if (Mandatory && UpdateMode == Mode.ForcedDownload)
                        {
                            DownloadUpdate(args);
                            Exit();
                        }
                        else
                        {
                            if (Thread.CurrentThread.GetApartmentState().Equals(ApartmentState.STA))
                            {
                                ShowUpdateForm(args);
                            }
                            else
                            {
                                Thread thread = new Thread(new ThreadStart(delegate { ShowUpdateForm(args); }));
                                thread.CurrentCulture       =
                                    thread.CurrentUICulture = CultureInfo.CurrentCulture;
                                thread.SetApartmentState(ApartmentState.STA);
                                thread.Start();
                                thread.Join();
                            }
                        }

                        return(true);
                    }

                    if (ReportErrors)
                    {
                        MessageBox.Show(Resources.UpdateUnavailableMessage,
                                        Resources.UpdateUnavailableCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

            return(false);
        }
示例#2
0
        private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            Assembly mainAssembly     = e.Argument as Assembly;
            var      companyAttribute =
                (AssemblyCompanyAttribute)GetAttribute(mainAssembly, typeof(AssemblyCompanyAttribute));
            var titleAttribute = (AssemblyTitleAttribute)GetAttribute(mainAssembly, typeof(AssemblyTitleAttribute));

            AppTitle = titleAttribute != null ? titleAttribute.Title : mainAssembly.GetName().Name;
            string appCompany = companyAttribute != null ? companyAttribute.Company : "";

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

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

            if (updateKey != null)
            {
                object remindLaterTime = updateKey.GetValue("remindlater");

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

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

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

            InstalledVersion = mainAssembly.GetName().Version;

            WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
            WebRequest webRequest = WebRequest.Create(AppCastURL);

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


            WebResponse webResponse;

            try
            {
                webResponse = webRequest.GetResponse();
            }
            catch (Exception)
            {
                if (null != CheckForUpdateEvent)
                {
                    CheckForUpdateEvent.Invoke(null);
                }
                return;
            }

            Stream appCastStream = webResponse.GetResponseStream();

            var receivedAppCastDocument = new XmlDocument();

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

            XmlNodeList appCastItems = receivedAppCastDocument.SelectNodes("XMLUpdateManifest");

            if (appCastItems != null)
            {
                foreach (XmlNode item in appCastItems)
                {
                    XmlNode appCastVersion = item.SelectSingleNode("version");
                    if (appCastVersion != null)
                    {
                        String appVersion = appCastVersion.InnerText;
                        CurrentVersion = new Version(appVersion);

                        if (CurrentVersion == null)
                        {
                            return;
                        }
                    }
                    else
                    {
                        continue;
                    }

                    XmlNode appCastChangeLog = item.SelectSingleNode("changelog");

                    ChangeLogURL = GetURL(webResponse.ResponseUri, appCastChangeLog);

                    XmlNode appCastUrl = item.SelectSingleNode("url");

                    DownloadURL = GetURL(webResponse.ResponseUri, appCastUrl);

                    if (IntPtr.Size.Equals(8))
                    {
                        XmlNode appCastUrl64 = item.SelectSingleNode("url64");

                        var downloadURL64 = GetURL(webResponse.ResponseUri, appCastUrl64);

                        if (!string.IsNullOrEmpty(downloadURL64))
                        {
                            DownloadURL = downloadURL64;
                        }
                    }

                    XmlNode xmlSignature = item.SelectSingleNode("signature");
                    if (xmlSignature != null)
                    {
                        Signature = xmlSignature.InnerText;
                    }
                }
            }

            if (updateKey != null)
            {
                object skip = updateKey.GetValue("skip");
                object applicationVersion = updateKey.GetValue("version");
                if (skip != null && applicationVersion != null)
                {
                    string skipValue   = skip.ToString();
                    var    skipVersion = new Version(applicationVersion.ToString());
                    if (skipValue.Equals("1") && CurrentVersion <= skipVersion)
                    {
                        return;
                    }
                    if (CurrentVersion > skipVersion)
                    {
                        RegistryKey updateKeyWrite = Registry.CurrentUser.CreateSubKey(RegistryLocation);
                        if (updateKeyWrite != null)
                        {
                            updateKeyWrite.SetValue("version", CurrentVersion.ToString());
                            updateKeyWrite.SetValue("skip", 0);
                        }
                    }
                }
                updateKey.Close();
            }

            var args = new UpdateInfoEventArgs
            {
                DownloadURL       = DownloadURL,
                ChangelogURL      = ChangeLogURL,
                CurrentVersion    = CurrentVersion,
                InstalledVersion  = InstalledVersion,
                IsUpdateAvailable = false,
            };

            if (CurrentVersion > InstalledVersion)
            {
                args.IsUpdateAvailable = true;
                if (CheckForUpdateEvent == null)
                {
                    var thread = new Thread(ShowUI);
                    thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                }
            }

            if (CheckForUpdateEvent != null)
            {
                CheckForUpdateEvent.Invoke(args);
            }
        }
示例#3
0
        private bool BindOption(Uri uri)
        {
            WebClient webClient = new WebClient
            {
                CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
            };

            if (UpdateContext.Proxy != null)
            {
                webClient.Proxy = UpdateContext.Proxy;
            }

            if (uri.Scheme.Equals(Uri.UriSchemeFtp))
            {
                webClient.Credentials = UpdateContext.FtpCredentials;
            }
            else
            {
                if (UpdateContext.RequestAuthorization != null)
                {
                    webClient.Headers[HttpRequestHeader.Authorization] = UpdateContext.RequestAuthorization.ToString();
                }

                webClient.Headers[HttpRequestHeader.UserAgent] = UpdateContext.HttpUserAgent;
            }

            try
            {
                var xmlFile = webClient.DownloadString(uri);

                if (string.IsNullOrEmpty(xmlFile))
                {
                    CheckForUpdateEvent?.Invoke(new AutoUpdateArgs
                    {
                        Message       = ConstResources.UpdateXmlFileEmpty,
                        UpdateContext = UpdateContext
                    });
                    return(false);
                }

                if (UpdateContext.UpdateOptionProvider == null)
                {
                    return(false);
                }

                UpdateContext.UpdateOption = UpdateContext.UpdateOptionProvider.ParseUpdateOption(xmlFile);

                UpdateContext.UpdateOption.InstalledVersion = GetAppVersion();
                //UpdateContext.UpdateOption.InstalledVersion = Assembly.GetEntryAssembly().GetName().Version;

                UpdateContext.UpdateOption.IsUpdateAvailable = UpdateContext.UpdateOption.UpdateVersion > UpdateContext.UpdateOption.InstalledVersion;
            }
            catch (WebException)
            {
                CheckForUpdateEvent?.Invoke(new AutoUpdateArgs
                {
                    Message       = ConstResources.UpdateXmlFileNotFound,
                    UpdateContext = UpdateContext
                });
                return(false);
            }
            catch (Exception)
            {
                CheckForUpdateEvent?.Invoke(new AutoUpdateArgs
                {
                    Message       = ConstResources.UpdateXmlFileEmpty,
                    UpdateContext = UpdateContext
                });
                return(false);
            }
            return(true);
        }