diff --git a/Common.csproj b/Common.csproj index e103dee..032b303 100644 --- a/Common.csproj +++ b/Common.csproj @@ -147,6 +147,11 @@ + + True + True + Resources.resx + @@ -204,6 +209,13 @@ + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs new file mode 100644 index 0000000..14b2b8f --- /dev/null +++ b/Properties/Resources.Designer.cs @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.34014 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Common.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Common.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to An update to version {0} is now available.. + /// + internal static string UpdateAvailable { + get { + return ResourceManager.GetString("UpdateAvailable", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to You are already running the most recent version. + /// + ///No updates are available at this time.. + /// + internal static string UpdateCheckCurrent { + get { + return ResourceManager.GetString("UpdateCheckCurrent", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Version {1} of {0} is now available. + /// + ///Would you like to download and install it now?. + /// + internal static string UpdateCheckNewVersion { + get { + return ResourceManager.GetString("UpdateCheckNewVersion", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to {0} Update. + /// + internal static string UpdateCheckTitle { + get { + return ResourceManager.GetString("UpdateCheckTitle", resourceCulture); + } + } + } +} diff --git a/Properties/Resources.resx b/Properties/Resources.resx new file mode 100644 index 0000000..48d63f7 --- /dev/null +++ b/Properties/Resources.resx @@ -0,0 +1,136 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Version {1} of {0} is now available. + +Would you like to download and install it now? + + + You are already running the most recent version. + +No updates are available at this time. + + + {0} Update + + + An update to version {0} is now available. + + \ No newline at end of file diff --git a/Update/UpdateCheck.cs b/Update/UpdateCheck.cs index 710f49b..6acd561 100644 --- a/Update/UpdateCheck.cs +++ b/Update/UpdateCheck.cs @@ -1,4 +1,5 @@ -using System; +using Common.Properties; +using System; using System.Diagnostics; using System.IO; using System.Net; @@ -9,28 +10,36 @@ namespace Common.Update { public static class UpdateCheck { - private static string _server; + public delegate void ApplicationShutdownDelegate(); + public delegate void ApplicationCurrentMessageDelegate(string title, string message); + public delegate bool ApplicationUpdateMessageDelegate(string title, string message); - public static VersionInfo VersionInfo { get; private set; } + public static ApplicationShutdownDelegate ApplicationShutdown = null; + public static ApplicationCurrentMessageDelegate ApplicationCurrentMessage = null; + public static ApplicationUpdateMessageDelegate ApplicationUpdateMessage = null; + + public static string UpdateServer; + public static string UpdateFile; + public static string ApplicationName { get; set; } + + public static VersionInfo RemoteVersion { get; private set; } public static string LocalInstallFile { get; private set; } public static bool UpdateAvailable { get; private set; } - public static Version CurrentVersion + public static Version LocalVersion { get { return Assembly.GetExecutingAssembly().GetName().Version; } } - public static bool CheckForUpdate(string server, string file) + public static bool CheckForUpdate() { - _server = server; + RemoteVersion = VersionInfo.Load(UpdateServer, UpdateFile); - VersionInfo = VersionInfo.Load(_server, file); - - if (VersionInfo == null) + if (RemoteVersion == null) return false; - var serverVersion = VersionInfo.Version; - var localVersion = CurrentVersion; + var serverVersion = RemoteVersion.Version; + var localVersion = LocalVersion; UpdateAvailable = serverVersion > localVersion; @@ -39,12 +48,12 @@ namespace Common.Update public static async Task DownloadUpdate() { - if (VersionInfo == null) + if (RemoteVersion == null) return false; - var remoteFile = _server + VersionInfo.InstallFile; + var remoteFile = UpdateServer + RemoteVersion.InstallFile; - LocalInstallFile = Path.Combine(Path.GetTempPath(), VersionInfo.InstallFile); + LocalInstallFile = Path.Combine(Path.GetTempPath(), RemoteVersion.InstallFile); var webClient = new WebClient(); @@ -55,12 +64,54 @@ namespace Common.Update public static bool InstallUpdate() { - if (VersionInfo == null) + if (RemoteVersion == null) return false; Process.Start(LocalInstallFile, "/passive"); return true; } + + public static async void DisplayUpdateInformation(bool showIfCurrent) + { + CheckForUpdate(); + + // Check for an update + if (UpdateAvailable) + { + // Load the version string from the server + var serverVersion = RemoteVersion.Version; + + // Format the check title + var updateCheckTitle = string.Format(Resources.UpdateCheckTitle, ApplicationName); + + // Format the message + var updateCheckMessage = string.Format(Resources.UpdateCheckNewVersion, ApplicationName, serverVersion); + + // Ask the user to update + if (ApplicationUpdateMessage(updateCheckTitle, updateCheckMessage)) + return; + + // Get the update + await DownloadUpdate(); + + // Start to install the update + InstallUpdate(); + + // Restart the application + ApplicationShutdown(); + } + else if (showIfCurrent) + { + // Format the check title + var updateCheckTitle = string.Format(Resources.UpdateCheckTitle, ApplicationName); + + // Format the message + var updateCheckMessage = string.Format(Resources.UpdateCheckCurrent, ApplicationName); + + ApplicationCurrentMessage(updateCheckTitle, updateCheckMessage); + } + } + } }