ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > DcUpdater

C# Updater class for DcuHelper

(1/1)

worstje:
Way back, around the time of the previous NANY release times, I once said to mouser that I would share the class I wrote to make use of dcuhelper.exe with the rest of DoCo. And then I forgot. Bad bad me. :-[

Anyhow, here it goes. I took this straight from JottiQ, so do with it what you wish.

Supported features of this class:

* Properties to abstract settings storage; by default it uses the WPF Settings store to remember whether it should automatically check for updates (Properties.Settings.Default.UpdaterEnabled) and when it last checked (Properties.Settings.Default.UpdaterLastCheck)
* Supports silent checking.
* Silent checking supports an interval, the default being one check every two days.
* Supports feeding additional parameters (JottiQ uses it to hide the Dcupdater button)
* Supports label name option.
* Supports custom captions for the dialogs (for when you hate mouser's addiction to default of an all caps UPDATE CHECK window caption).
Code of Updater class: (save in Updater.cs)

--- Code: C# ---using System;using System.Windows;using System.Diagnostics;using System.Reflection; namespace JottiQ{    public static class Updater    {        /* Settings - adjust as necessary. */        private static string FileUpdater = "dcuhelper.exe";        private static string FileConfig = "JottiQ.dcupdate";         private static string UpdaterLabelName = "JottiQ";        private static string UpdaterDialogCaption = "Update check";        private static string UpdaterAdditionalFlags = "-dontofferdcupdaterpage";         private static int UpdaterCheckingInterval = 2;    /* days since last check */         private static string UpdaterNotAvailableCaption = "You silly monkey.";        private static string UpdaterNotAvailableMessage = "Updater support is not present.";         /* Interface for persistent storage so we don't litter the rest of the code         * with hard-coded references to the Settings stuff.         */        private static bool UpdaterEnabled        {            get { return Properties.Settings.Default.UpdaterEnabled; }        }         private static DateTime UpdaterLastCheck        {            get { return Properties.Settings.Default.UpdaterLastCheck; }            set            {                Properties.Settings.Default.UpdaterLastCheck = value;                Properties.Settings.Default.Save();            }        }         /* Implementation of the class goes below here - no changes needed. */        private static String GetInstallationLocation()        {            string appDir = Assembly.GetExecutingAssembly().Location;            appDir = System.IO.Path.GetDirectoryName(appDir);            if (!appDir.EndsWith(@"\"))                appDir = appDir + @"\";             return appDir;        }         public static bool IsUpdaterAvailable()        {            bool dcUpdaterAvailable =                System.IO.File.Exists(GetInstallationLocation() + FileUpdater) &&                System.IO.File.Exists(GetInstallationLocation() + FileConfig);             return dcUpdaterAvailable;        }         public static void CheckForUpdates(bool explicitCheck)        {            if (!explicitCheck)            {                /* See if we are allowed to do automatic checks. */                if (UpdaterEnabled == false)                    return;                 /* See if last check was less than X days ago. */                TimeSpan ts = DateTime.Now.Subtract(UpdaterLastCheck);                if (ts.Days < UpdaterCheckingInterval)                    return;            }             if (IsUpdaterAvailable())            {                System.Diagnostics.ProcessStartInfo updater =                    new System.Diagnostics.ProcessStartInfo();                updater.FileName = GetInstallationLocation() + FileUpdater;                updater.WorkingDirectory = GetInstallationLocation();                updater.Arguments = "-i \"" + UpdaterLabelName + "\" \".\" \"" + (explicitCheck ? UpdaterDialogCaption : ".") + "\" " + UpdaterAdditionalFlags;                 /* Old versions of dcuhelper.exe pop up an ugly command-line window. Setting the WindowStyle to Hidden hides that ugly monstrosity.                 *                  * PLEASE REMOVE this line if you have a recent version of dcuhelper.exe (later than ~January 1, 2011), since at some point                 * mouser redesigned the dialog in dcuhelper.exe, which actually broke the Hidden trick. At July 12, this bug was found and fixed,                 * so the Hidden switch can once again safely be used with dcuhelper.exe - although it is pointless as the ugly monstrosity has                 * been amputated somewhere along the way. Oh joy.                 *                  * Short story: safe for dcuhelper.exe versions: <= v1.05.01 (Oct 8, 2010) && >= v1.10.01 (July 12, 2011)                 */                updater.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;                 /* Finally start dcuhelper.exe to do our check. */                Process.Start(updater);                 /* Make sure to note down that we checked for an update. */                UpdaterLastCheck = DateTime.Now;            }            else if (explicitCheck)                MessageBox.Show(UpdaterNotAvailableMessage, UpdaterNotAvailableCaption, MessageBoxButton.OK, MessageBoxImage.Error);        }    }}
To use it, you basically want to adjust the top parameters. Sadly enough, dcuhelper.exe is a total mess for as far its commandline interface goes, which is why I hope the above settings are a good example for people to start out with. If you want to urge your users to use dcupdater, by all means remove the additional parameter -dontofferdcupdaterpage that I once upon a time forced mouser to add. Likewise, you may (or may not) want to change the implementation of the properties the Updater class uses to implement its logic.

From there on forth, it is simple. On application start (or preferably MainWindow_Loaded given that that means you've gotten through to a visual interface), call:


--- Code: C# ---/* Check for updates. */Updater.CheckForUpdates(false);
The false parameter means it is not an explicit check. In comparison, the JottiQ Updates? button is rigged the opposite way to indicate that the user initiated the update check:


--- Code: C# ---private void checkForUpdatesButton_Click(object sender, RoutedEventArgs e){    Updater.CheckForUpdates(true);}
And prior to that, we make sure the button is only enabled when the appropriate files are in place. (In case some required files are not present.)


--- Code: C# ---private void Window_Loaded(object sender, RoutedEventArgs e){    /* If we have update functionality, enable the button. */    checkForUpdatesButton.IsEnabled = JottiQ.Updater.IsUpdaterAvailable();}
Any comments and suggestions, please share. :)

Ath:
Hey, that's nice code, thanks :Thmbsup:
I'll be using that in a DC-related project soon, I hope. :tellme:

Navigation

[0] Message Index

Go to full version