Example #1
0
 public static void OnUpdateStatus(AutoUpdateEventArgs e)
 {
     if (UpdateStatus != null)
     {
         UpdateStatus(null, e);  // this, e
     }
 }
Example #2
0
        private static void TraceWriteLine(String s)
        {
            AutoUpdateEventArgs args = new AutoUpdateEventArgs
            {
                Status = AutoUpdateEventArgs.StatusCodes.trace,
                Text   = s
            };

            OnUpdateStatus(args);
        }
Example #3
0
        private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            int compareResult;
            AutoUpdateEventArgs args = new AutoUpdateEventArgs();

            Thread.CurrentThread.CurrentUICulture = CurrentCulture;
            var mainAssembly = Assembly.GetEntryAssembly();
            var companyAttribute = (AssemblyCompanyAttribute)GetAttribute(mainAssembly, typeof(AssemblyCompanyAttribute));
            var titleAttribute = (AssemblyTitleAttribute)GetAttribute(mainAssembly, typeof(AssemblyTitleAttribute));
            AppTitle = titleAttribute != null ? titleAttribute.Title : mainAssembly.GetName().Name;
            var appCompany = companyAttribute != null ? companyAttribute.Company : "";
            int days = (int)e.Argument;

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

            RegistryKey updateKey = Registry.CurrentUser.OpenSubKey(RegistryLocation, true) ??
                                    Registry.CurrentUser.CreateSubKey(RegistryLocation);

            object remindLaterTime = updateKey.GetValue("remindlater");
            if (remindLaterTime == null && days > 0)
            {
                string lastcheck = updateKey.GetValue("lastcheck", "").ToString();
                if (!String.IsNullOrEmpty(lastcheck))
                {
                    try
                    {
                        DateTime lastcheckDate = Convert.ToDateTime(lastcheck, CultureInfo.InvariantCulture);
                        compareResult = DateTime.Compare(DateTime.Now, lastcheckDate.AddDays(days));

                        if (compareResult < 0)
                        {
                            args.Status = AutoUpdateEventArgs.StatusCodes.delayed;
                            args.Text = string.Format(Strings.sLastCheck, lastcheckDate.ToString("D", CurrentCulture));
                            OnUpdateStatus(args);
                            return;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            if (remindLaterTime != null && days > 0)
            {
                try
                {
                    DateTime remindLater = Convert.ToDateTime(remindLaterTime.ToString(), CultureInfo.InvariantCulture);

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

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

            if (remindLaterTime != null)
                updateKey.DeleteValue("remindlater");

            InstalledVersion = mainAssembly.GetName().Version;

            args.Status = AutoUpdateEventArgs.StatusCodes.checking;
            args.Text = Strings.sCheckingForUpdates;
            OnUpdateStatus(args);
            WebRequest webRequest = WebRequest.Create(AppCastURL);
            webRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

            WebResponse webResponse;

            try
            {
                webResponse = webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.error;
                args.Text = ex.Message;
                OnUpdateStatus(args);
                return;
            }

            Stream appCastStream = webResponse.GetResponseStream();

            var receivedAppCastDocument = new XmlDocument();

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

            XmlNodeList appCastItems = receivedAppCastDocument.SelectNodes("channel/item");

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

                    XmlNode appCastTitle = item.SelectSingleNode("title");

                    DialogTitle = appCastTitle != null ? appCastTitle.InnerText : "";

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

                    ChangeLogURL = appCastChangeLog != null ? appCastChangeLog.InnerText : "";

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

                    DownloadURL = appCastUrl != null ? appCastUrl.InnerText : "";
                }
            }

            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)
                {
                    args.Status = AutoUpdateEventArgs.StatusCodes.noUpdateAvailable;
                    args.Text = Strings.sSkipping;
                    OnUpdateStatus(args);
                    return;
                }
                if (CurrentVersion > skipVersion)
                {
                    RegistryKey updateKeyWrite = Registry.CurrentUser.CreateSubKey(RegistryLocation);
                    if (updateKeyWrite != null)
                    {
                        updateKeyWrite.SetValue("version", CurrentVersion.ToString());
                        updateKeyWrite.SetValue("skip", 0);
                    }
                }
            }

            updateKey.SetValue("lastcheck", DateTime.Now.ToString(CultureInfo.InvariantCulture));
            updateKey.Close();
            RunBrowserThread(versionURL);

            #if DEBUG
            if (days == -1)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.updateAvailable;
                args.Text = Strings.sUpdateAvailable;
                OnUpdateStatus(args);

                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
            #endif

            if (CurrentVersion == null)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.noUpdateAvailable;
                args.Text = Strings.sLatestVersion;
                OnUpdateStatus(args);
                return;
            }

            if (CurrentVersion <= InstalledVersion) return;
            args.Status = AutoUpdateEventArgs.StatusCodes.updateAvailable;
            args.Text = Strings.sUpdateAvailable;
            OnUpdateStatus(args);

            if (days == 0)
            {
                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
        }
Example #4
0
        private static void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            int compareResult;
            AutoUpdateEventArgs args = new AutoUpdateEventArgs();

            TraceWriteLine("Beginning AutoUpdater worker");

            Thread.CurrentThread.CurrentUICulture = CurrentCulture;
            var mainAssembly     = Assembly.GetEntryAssembly();
            var companyAttribute = (AssemblyCompanyAttribute)GetAttribute(mainAssembly, typeof(AssemblyCompanyAttribute));
            var titleAttribute   = (AssemblyTitleAttribute)GetAttribute(mainAssembly, typeof(AssemblyTitleAttribute));

            AppTitle = titleAttribute != null ? titleAttribute.Title : mainAssembly.GetName().Name;
            var appCompany = companyAttribute != null ? companyAttribute.Company : "";
            int days       = (int)e.Argument;

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

            RegistryKey updateKey = Registry.CurrentUser.OpenSubKey(RegistryLocation, true) ??
                                    Registry.CurrentUser.CreateSubKey(RegistryLocation);

            object remindLaterTime = updateKey.GetValue("remindlater");

            if (remindLaterTime == null && days > 0)
            {
                string lastcheck = updateKey.GetValue("lastcheck", "").ToString();
                if (!String.IsNullOrEmpty(lastcheck))
                {
                    try
                    {
                        DateTime lastcheckDate = Convert.ToDateTime(lastcheck, CultureInfo.InvariantCulture);
                        compareResult = DateTime.Compare(DateTime.Now, lastcheckDate.AddDays(days));

                        if (compareResult < 0)
                        {
                            args.Status = AutoUpdateEventArgs.StatusCodes.delayed;
                            args.Text   = string.Format(Strings.sLastCheck, lastcheckDate.ToString("D", CurrentCulture));
                            OnUpdateStatus(args);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceWriteLine("AutoUpdater(175)::" + ex.Message);
                    }
                }
            }

            if (remindLaterTime != null && days > 0)
            {
                try
                {
                    DateTime remindLater = Convert.ToDateTime(remindLaterTime.ToString(), CultureInfo.InvariantCulture);

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

                    if (compareResult < 0)
                    {
                        var updateForm = new UpdateForm(true);
                        updateForm.SetTimer(remindLater);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    TraceWriteLine("AutoUpdater(198)::" + ex.Message);
                }
            }

            if (remindLaterTime != null)
            {
                updateKey.DeleteValue("remindlater");
            }

            InstalledVersion = mainAssembly.GetName().Version;

            args.Status = AutoUpdateEventArgs.StatusCodes.checking;
            args.Text   = Strings.sCheckingForUpdates;
            OnUpdateStatus(args);
            WebRequest webRequest = WebRequest.Create(AppCastURL);

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

            WebResponse webResponse;

            try
            {
                webResponse = webRequest.GetResponse();
            }
            catch (Exception ex)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.error;
                args.Text   = ex.Message;
                OnUpdateStatus(args);
                return;
            }

            Stream appCastStream = webResponse.GetResponseStream();

            var receivedAppCastDocument = new XmlDocument();

            if (appCastStream != null)
            {
                receivedAppCastDocument.Load(appCastStream);
            }
            else
            {
                TraceWriteLine("AutoUpdater(234)::return");
                return;
            }

            XmlNodeList appCastItems = receivedAppCastDocument.SelectNodes("channel/item");

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

                        XmlNode appCastTitle = item.SelectSingleNode("title");

                        DialogTitle = appCastTitle != null ? appCastTitle.InnerText : "";

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

                        ChangeLogURL = appCastChangeLog != null ? appCastChangeLog.InnerText : "";

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

                        DownloadURL = appCastUrl != null ? appCastUrl.InnerText : "";
                    }
                }
            }

            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") && InstalledVersion == skipVersion)
                {
                    args.Status = AutoUpdateEventArgs.StatusCodes.noUpdateAvailable;
                    args.Text   = Strings.sSkipping;
                    OnUpdateStatus(args);
                    return;
                }
                if (InstalledVersion > skipVersion)
                {
                    RegistryKey updateKeyWrite = Registry.CurrentUser.CreateSubKey(RegistryLocation);
                    if (updateKeyWrite != null)
                    {
                        updateKeyWrite.SetValue("version", InstalledVersion.ToString());
                        updateKeyWrite.SetValue("skip", 0);
                    }
                }
            }

            updateKey.SetValue("lastcheck", DateTime.Now.ToString(CultureInfo.InvariantCulture));
            updateKey.Close();
            RunBrowserThread(versionURL);

#if DEBUG
            if (force)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.updateAvailable;
                args.Text   = Strings.sUpdateAvailable;
                OnUpdateStatus(args);

                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                return;
            }
#endif

            if (InstalledVersion >= AvailableVersion)
            {
                args.Status = AutoUpdateEventArgs.StatusCodes.noUpdateAvailable;
                args.Text   = Strings.sLatestVersion;
                OnUpdateStatus(args);
                return;
            }

            args.Status = AutoUpdateEventArgs.StatusCodes.updateAvailable;
            args.Text   = Strings.sUpdateAvailable;
            OnUpdateStatus(args);

            if (days == 0)
            {
                var thread = new Thread(ShowUI);
                thread.CurrentCulture = thread.CurrentUICulture = CurrentCulture ?? Application.CurrentCulture;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
            }
        }
Example #5
0
 public static void OnUpdateStatus(AutoUpdateEventArgs e)
 {
     if (UpdateStatus != null)
         UpdateStatus(null, e);  // this, e
 }