From 0e5305063549ff9eb9003efc6b38ece09c5da59e Mon Sep 17 00:00:00 2001 From: CodeDead Date: Tue, 21 May 2019 13:43:11 +0200 Subject: [PATCH] * Added JSON support * Refactoring * Version bump --- Package.nuspec | 5 +- README.md | 26 ++++++- UpdateManager/Classes/DataType.cs | 11 +++ UpdateManager/Classes/UpdateManager.cs | 89 ++++++++++++++++-------- UpdateManager/Properties/AssemblyInfo.cs | 4 +- UpdateManager/UpdateManager.csproj | 6 ++ 6 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 UpdateManager/Classes/DataType.cs diff --git a/Package.nuspec b/Package.nuspec index 73062d1..3baa187 100644 --- a/Package.nuspec +++ b/Package.nuspec @@ -2,7 +2,7 @@ CodeDead.UpdateManager - 1.4.4 + 1.5 CodeDead CodeDead LICENSE.txt @@ -14,8 +14,7 @@ This library can be used to check for application updates. It is designed for WPF and Windows Forms applications. In order to use it, you require an XML file on a remote or local server that represents the Update class. - * Upgrade to .NET Framework 4.8 - * Code refactoring + Added JSON support and some refactoring Copyright © 2019 CodeDead CodeDead UpdateManager Update Updater updates updating downloader diff --git a/README.md b/README.md index 23eeefd..8c9cb4e 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ StringVariables stringVariables = new StringVariables TitleText = "Your application title", UpdateNowText = "Would you like to update the application now?" }; -UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables); +UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml); ``` Check for updates like this: @@ -39,8 +39,30 @@ catch (Exception ex) MessageBox.Show(ex.Message, "Application title", MessageBoxButton.OK, MessageBoxImage.Error); } ``` +## Update types +Updates can be stored on your server in two different formats: *JSON* or *XML*. Be sure to set the right DataType when initializing the UpdateManager: +```C# +UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml); +``` + +```C# +UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Json); +``` + +### JSON example +```JSON +{ + "MajorVersion": 1, + "MinorVersion": 0, + "BuildVersion": 0, + "RevisionVersion": 0, + "UpdateUrl": "https://example.com/update.exe", + "InfoUrl": "https://codedead.com", + "UpdateInfo": "A new version is now available. Please click the download button to download version 1.0.0.0" +} +``` -## Update XML example +### XML example ```XML diff --git a/UpdateManager/Classes/DataType.cs b/UpdateManager/Classes/DataType.cs new file mode 100644 index 0000000..d8ee1b7 --- /dev/null +++ b/UpdateManager/Classes/DataType.cs @@ -0,0 +1,11 @@ +namespace CodeDead.UpdateManager.Classes +{ + /// + /// Enum that contains the different data types that are supported in order to deserialize the Update information + /// + public enum DataType + { + Json, + Xml + } +} diff --git a/UpdateManager/Classes/UpdateManager.cs b/UpdateManager/Classes/UpdateManager.cs index 6a8a444..fc4e17b 100644 --- a/UpdateManager/Classes/UpdateManager.cs +++ b/UpdateManager/Classes/UpdateManager.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Net; +using System.Web.Script.Serialization; using System.Windows; using System.Xml.Serialization; using CodeDead.UpdateManager.Windows; @@ -18,10 +19,6 @@ public sealed class UpdateManager /// private readonly string _updateUrl; /// - /// The Update object containing data about the current update - /// - private Update _update; - /// /// The version of the application /// private readonly Version _applicationVersion; @@ -29,6 +26,10 @@ public sealed class UpdateManager /// The string variables that can be used to display information to the user /// private StringVariables _stringVariables; + /// + /// The DataType that can be used to deserialize the update information + /// + private readonly DataType _dataType; #endregion /// @@ -37,14 +38,12 @@ public sealed class UpdateManager /// Your application version /// The URL where your XML update file is located /// StringVariables object containing strings that can be used to display information to the user - public UpdateManager(Version version, string updateUrl, StringVariables stringVariables) + /// The DataType that can be used to deserialize the update information + public UpdateManager(Version version, string updateUrl, StringVariables stringVariables, DataType datatype) { _updateUrl = updateUrl; - - _update = new Update(); + _dataType = datatype; _applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision); - - _update.SetApplicationVersion(_applicationVersion); SetStringVariables(stringVariables); } @@ -53,14 +52,12 @@ public UpdateManager(Version version, string updateUrl, StringVariables stringVa /// /// Your application version /// The URL where your XML update file is located - public UpdateManager(Version version, string updateUrl) + /// The DataType that can be used to deserialize the update information + public UpdateManager(Version version, string updateUrl, DataType dataType) { _updateUrl = updateUrl; - - _update = new Update(); + _dataType = dataType; _applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision); - - _update.SetApplicationVersion(_applicationVersion); SetStringVariables(new StringVariables()); } @@ -73,31 +70,30 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates) { try { - string xml = await new WebClient().DownloadStringTaskAsync(_updateUrl); - - XmlSerializer serializer = new XmlSerializer(_update.GetType()); - using (MemoryStream stream = new MemoryStream()) + string data = await new WebClient().DownloadStringTaskAsync(_updateUrl); + Update update; + switch (_dataType) { - StreamWriter writer = new StreamWriter(stream); - writer.Write(xml); - writer.Flush(); - stream.Position = 0; - _update = (Update)serializer.Deserialize(stream); - _update.SetApplicationVersion(_applicationVersion); - writer.Dispose(); + default: + update = DeserializeJson(data); + break; + case DataType.Xml: + update = DeserializeXml(data); + break; } - if (_update.CheckForUpdate()) + + if (update.CheckForUpdate()) { UpdateWindow window = new UpdateWindow { Title = _stringVariables.TitleText, - InformationTextBlockContent = _update.UpdateInfo, + InformationTextBlockContent = update.UpdateInfo, InformationButtonContent = _stringVariables.InformationButtonText, CancelButtonContent = _stringVariables.CancelButtonText, DownloadButtonContent = _stringVariables.DownloadButtonText, - DownloadUrl = _update.UpdateUrl, - InformationUrl = _update.InfoUrl, + DownloadUrl = update.UpdateUrl, + InformationUrl = update.InfoUrl, UpdateNowText = _stringVariables.UpdateNowText }; window.ShowDialog(); @@ -119,6 +115,41 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates) } } + /// + /// Deserialize the XML data into an Update object + /// + /// The XML data that should be deserialized + /// The Update object that was deserialized + private Update DeserializeXml(string data) + { + Update update; + XmlSerializer serializer = new XmlSerializer(typeof(Update)); + using (MemoryStream stream = new MemoryStream()) + { + StreamWriter writer = new StreamWriter(stream); + writer.Write(data); + writer.Flush(); + stream.Position = 0; + update = (Update)serializer.Deserialize(stream); + update.SetApplicationVersion(_applicationVersion); + writer.Dispose(); + } + + return update; + } + + /// + /// Deserialize the Json data into an Update object + /// + /// The Json data that should be deserialized + /// The Update object that was deserialized + private Update DeserializeJson(string data) + { + Update update = new JavaScriptSerializer().Deserialize(data); + update.SetApplicationVersion(_applicationVersion); + return update; + } + /// /// Change the StringVariables during runtime /// diff --git a/UpdateManager/Properties/AssemblyInfo.cs b/UpdateManager/Properties/AssemblyInfo.cs index 298000d..1ed3503 100644 --- a/UpdateManager/Properties/AssemblyInfo.cs +++ b/UpdateManager/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.4.4.0")] -[assembly: AssemblyFileVersion("1.4.4.0")] +[assembly: AssemblyVersion("1.5.0.0")] +[assembly: AssemblyFileVersion("1.5.0.0")] diff --git a/UpdateManager/UpdateManager.csproj b/UpdateManager/UpdateManager.csproj index 1d2d157..a77582a 100644 --- a/UpdateManager/UpdateManager.csproj +++ b/UpdateManager/UpdateManager.csproj @@ -30,17 +30,23 @@ prompt 4 + + + + + +