Skip to content

Commit 0e53050

Browse files
committed
* Added JSON support
* Refactoring * Version bump
1 parent a862fbf commit 0e53050

File tree

6 files changed

+105
-36
lines changed

6 files changed

+105
-36
lines changed

Package.nuspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata>
44
<id>CodeDead.UpdateManager</id>
5-
<version>1.4.4</version>
5+
<version>1.5</version>
66
<authors>CodeDead</authors>
77
<owners>CodeDead</owners>
88
<license type="file">LICENSE.txt</license>
@@ -14,8 +14,7 @@
1414
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.
1515
</description>
1616
<releaseNotes>
17-
* Upgrade to .NET Framework 4.8
18-
* Code refactoring
17+
Added JSON support and some refactoring
1918
</releaseNotes>
2019
<copyright>Copyright © 2019 CodeDead</copyright>
2120
<tags>CodeDead UpdateManager Update Updater updates updating downloader</tags>

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ StringVariables stringVariables = new StringVariables
2525
TitleText = "Your application title",
2626
UpdateNowText = "Would you like to update the application now?"
2727
};
28-
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables);
28+
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml);
2929
```
3030

3131
Check for updates like this:
@@ -39,8 +39,30 @@ catch (Exception ex)
3939
MessageBox.Show(ex.Message, "Application title", MessageBoxButton.OK, MessageBoxImage.Error);
4040
}
4141
```
42+
## Update types
43+
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:
44+
```C#
45+
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Xml);
46+
```
47+
48+
```C#
49+
UpdateManager updateManager = new UpdateManager(Assembly.GetExecutingAssembly().GetName().Version, "https://yoururl/update.xml", stringVariables, DataType.Json);
50+
```
51+
52+
### JSON example
53+
```JSON
54+
{
55+
"MajorVersion": 1,
56+
"MinorVersion": 0,
57+
"BuildVersion": 0,
58+
"RevisionVersion": 0,
59+
"UpdateUrl": "https://example.com/update.exe",
60+
"InfoUrl": "https://codedead.com",
61+
"UpdateInfo": "A new version is now available. Please click the download button to download version 1.0.0.0"
62+
}
63+
```
4264

43-
## Update XML example
65+
### XML example
4466
```XML
4567
<?xml version="1.0"?>
4668
<Update xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

UpdateManager/Classes/DataType.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace CodeDead.UpdateManager.Classes
2+
{
3+
/// <summary>
4+
/// Enum that contains the different data types that are supported in order to deserialize the Update information
5+
/// </summary>
6+
public enum DataType
7+
{
8+
Json,
9+
Xml
10+
}
11+
}

UpdateManager/Classes/UpdateManager.cs

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Net;
4+
using System.Web.Script.Serialization;
45
using System.Windows;
56
using System.Xml.Serialization;
67
using CodeDead.UpdateManager.Windows;
@@ -18,17 +19,17 @@ public sealed class UpdateManager
1819
/// </summary>
1920
private readonly string _updateUrl;
2021
/// <summary>
21-
/// The Update object containing data about the current update
22-
/// </summary>
23-
private Update _update;
24-
/// <summary>
2522
/// The version of the application
2623
/// </summary>
2724
private readonly Version _applicationVersion;
2825
/// <summary>
2926
/// The string variables that can be used to display information to the user
3027
/// </summary>
3128
private StringVariables _stringVariables;
29+
/// <summary>
30+
/// The DataType that can be used to deserialize the update information
31+
/// </summary>
32+
private readonly DataType _dataType;
3233
#endregion
3334

3435
/// <summary>
@@ -37,14 +38,12 @@ public sealed class UpdateManager
3738
/// <param name="version">Your application version</param>
3839
/// <param name="updateUrl">The URL where your XML update file is located</param>
3940
/// <param name="stringVariables">StringVariables object containing strings that can be used to display information to the user</param>
40-
public UpdateManager(Version version, string updateUrl, StringVariables stringVariables)
41+
/// <param name="datatype">The DataType that can be used to deserialize the update information</param>
42+
public UpdateManager(Version version, string updateUrl, StringVariables stringVariables, DataType datatype)
4143
{
4244
_updateUrl = updateUrl;
43-
44-
_update = new Update();
45+
_dataType = datatype;
4546
_applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision);
46-
47-
_update.SetApplicationVersion(_applicationVersion);
4847
SetStringVariables(stringVariables);
4948
}
5049

@@ -53,14 +52,12 @@ public UpdateManager(Version version, string updateUrl, StringVariables stringVa
5352
/// </summary>
5453
/// <param name="version">Your application version</param>
5554
/// <param name="updateUrl">The URL where your XML update file is located</param>
56-
public UpdateManager(Version version, string updateUrl)
55+
/// <param name="dataType">The DataType that can be used to deserialize the update information</param>
56+
public UpdateManager(Version version, string updateUrl, DataType dataType)
5757
{
5858
_updateUrl = updateUrl;
59-
60-
_update = new Update();
59+
_dataType = dataType;
6160
_applicationVersion = new Version(version.Major, version.Minor, version.Build, version.Revision);
62-
63-
_update.SetApplicationVersion(_applicationVersion);
6461
SetStringVariables(new StringVariables());
6562
}
6663

@@ -73,31 +70,30 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
7370
{
7471
try
7572
{
76-
string xml = await new WebClient().DownloadStringTaskAsync(_updateUrl);
77-
78-
XmlSerializer serializer = new XmlSerializer(_update.GetType());
79-
using (MemoryStream stream = new MemoryStream())
73+
string data = await new WebClient().DownloadStringTaskAsync(_updateUrl);
74+
Update update;
75+
switch (_dataType)
8076
{
81-
StreamWriter writer = new StreamWriter(stream);
82-
writer.Write(xml);
83-
writer.Flush();
84-
stream.Position = 0;
85-
_update = (Update)serializer.Deserialize(stream);
86-
_update.SetApplicationVersion(_applicationVersion);
87-
writer.Dispose();
77+
default:
78+
update = DeserializeJson(data);
79+
break;
80+
case DataType.Xml:
81+
update = DeserializeXml(data);
82+
break;
8883
}
8984

90-
if (_update.CheckForUpdate())
85+
86+
if (update.CheckForUpdate())
9187
{
9288
UpdateWindow window = new UpdateWindow
9389
{
9490
Title = _stringVariables.TitleText,
95-
InformationTextBlockContent = _update.UpdateInfo,
91+
InformationTextBlockContent = update.UpdateInfo,
9692
InformationButtonContent = _stringVariables.InformationButtonText,
9793
CancelButtonContent = _stringVariables.CancelButtonText,
9894
DownloadButtonContent = _stringVariables.DownloadButtonText,
99-
DownloadUrl = _update.UpdateUrl,
100-
InformationUrl = _update.InfoUrl,
95+
DownloadUrl = update.UpdateUrl,
96+
InformationUrl = update.InfoUrl,
10197
UpdateNowText = _stringVariables.UpdateNowText
10298
};
10399
window.ShowDialog();
@@ -119,6 +115,41 @@ public async void CheckForUpdate(bool showErrors, bool showNoUpdates)
119115
}
120116
}
121117

118+
/// <summary>
119+
/// Deserialize the XML data into an Update object
120+
/// </summary>
121+
/// <param name="data">The XML data that should be deserialized</param>
122+
/// <returns>The Update object that was deserialized</returns>
123+
private Update DeserializeXml(string data)
124+
{
125+
Update update;
126+
XmlSerializer serializer = new XmlSerializer(typeof(Update));
127+
using (MemoryStream stream = new MemoryStream())
128+
{
129+
StreamWriter writer = new StreamWriter(stream);
130+
writer.Write(data);
131+
writer.Flush();
132+
stream.Position = 0;
133+
update = (Update)serializer.Deserialize(stream);
134+
update.SetApplicationVersion(_applicationVersion);
135+
writer.Dispose();
136+
}
137+
138+
return update;
139+
}
140+
141+
/// <summary>
142+
/// Deserialize the Json data into an Update object
143+
/// </summary>
144+
/// <param name="data">The Json data that should be deserialized</param>
145+
/// <returns>The Update object that was deserialized</returns>
146+
private Update DeserializeJson(string data)
147+
{
148+
Update update = new JavaScriptSerializer().Deserialize<Update>(data);
149+
update.SetApplicationVersion(_applicationVersion);
150+
return update;
151+
}
152+
122153
/// <summary>
123154
/// Change the StringVariables during runtime
124155
/// </summary>

UpdateManager/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.4.4.0")]
35-
[assembly: AssemblyFileVersion("1.4.4.0")]
34+
[assembly: AssemblyVersion("1.5.0.0")]
35+
[assembly: AssemblyFileVersion("1.5.0.0")]

UpdateManager/UpdateManager.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@
3030
<ErrorReport>prompt</ErrorReport>
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
33+
<PropertyGroup>
34+
<StartupObject>
35+
</StartupObject>
36+
</PropertyGroup>
3337
<ItemGroup>
3438
<Reference Include="PresentationCore" />
3539
<Reference Include="PresentationFramework" />
3640
<Reference Include="System" />
3741
<Reference Include="System.Core" />
42+
<Reference Include="System.Web.Extensions" />
3843
<Reference Include="System.Windows" />
3944
<Reference Include="System.Xaml" />
4045
<Reference Include="System.Xml" />
4146
<Reference Include="WindowsBase" />
4247
</ItemGroup>
4348
<ItemGroup>
49+
<Compile Include="Classes\DataType.cs" />
4450
<Compile Include="Classes\StringVariables.cs" />
4551
<Compile Include="Properties\AssemblyInfo.cs" />
4652
<Compile Include="Classes\Update.cs" />

0 commit comments

Comments
 (0)