mirror of
https://github.com/ckaczor/Common.git
synced 2026-01-13 17:22:40 -05:00
Add update checking
This commit is contained in:
@@ -141,6 +141,7 @@
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xaml" />
|
||||
<Reference Include="System.XML" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@@ -162,6 +163,8 @@
|
||||
<Compile Include="Trace\Tracer.cs" />
|
||||
<Compile Include="Settings\Extensions.cs" />
|
||||
<Compile Include="Settings\GenericSettingsProvider.cs" />
|
||||
<Compile Include="Update\UpdateCheck.cs" />
|
||||
<Compile Include="Update\VersionInfo.cs" />
|
||||
<Compile Include="Xml\XmlExtensions.cs" />
|
||||
<AppDesigner Include="Properties\" />
|
||||
<Compile Include="Xml\XmlSanitizingStream.cs" />
|
||||
|
||||
66
Update/UpdateCheck.cs
Normal file
66
Update/UpdateCheck.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Common.Update
|
||||
{
|
||||
public static class UpdateCheck
|
||||
{
|
||||
private static string _server;
|
||||
|
||||
public static VersionInfo VersionInfo { get; private set; }
|
||||
public static string LocalInstallFile { get; private set; }
|
||||
public static bool UpdateAvailable { get; private set; }
|
||||
|
||||
public static Version CurrentVersion
|
||||
{
|
||||
get { return Assembly.GetExecutingAssembly().GetName().Version; }
|
||||
}
|
||||
|
||||
public static bool CheckForUpdate(string server, string file)
|
||||
{
|
||||
_server = server;
|
||||
|
||||
VersionInfo = VersionInfo.Load(_server, file);
|
||||
|
||||
if (VersionInfo == null)
|
||||
return false;
|
||||
|
||||
var serverVersion = VersionInfo.Version;
|
||||
var localVersion = CurrentVersion;
|
||||
|
||||
UpdateAvailable = serverVersion > localVersion;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static async Task<bool> DownloadUpdate()
|
||||
{
|
||||
if (VersionInfo == null)
|
||||
return false;
|
||||
|
||||
var remoteFile = _server + VersionInfo.InstallFile;
|
||||
|
||||
LocalInstallFile = Path.Combine(Path.GetTempPath(), VersionInfo.InstallFile);
|
||||
|
||||
var webClient = new WebClient();
|
||||
|
||||
await webClient.DownloadFileTaskAsync(new Uri(remoteFile), LocalInstallFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool InstallUpdate()
|
||||
{
|
||||
if (VersionInfo == null)
|
||||
return false;
|
||||
|
||||
Process.Start(LocalInstallFile, "/passive");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Update/VersionInfo.cs
Normal file
48
Update/VersionInfo.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Common.Debug;
|
||||
using System;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Common.Update
|
||||
{
|
||||
public class VersionInfo
|
||||
{
|
||||
public Version Version { get; set; }
|
||||
public string InstallFile { get; set; }
|
||||
public DateTime InstallCreated { get; set; }
|
||||
|
||||
public static VersionInfo Load(string server, string file)
|
||||
{
|
||||
try
|
||||
{
|
||||
var document = XDocument.Load(server + file);
|
||||
|
||||
var versionInformationElement = document.Element("versionInformation");
|
||||
|
||||
if (versionInformationElement == null)
|
||||
return null;
|
||||
|
||||
var versionElement = versionInformationElement.Element("version");
|
||||
var installFileElement = versionInformationElement.Element("installFile");
|
||||
var installCreatedElement = versionInformationElement.Element("installCreated");
|
||||
|
||||
if (versionElement == null || installFileElement == null || installCreatedElement == null)
|
||||
return null;
|
||||
|
||||
var versionInfo = new VersionInfo
|
||||
{
|
||||
Version = Version.Parse(versionElement.Value),
|
||||
InstallFile = installFileElement.Value,
|
||||
InstallCreated = DateTime.Parse(installCreatedElement.Value)
|
||||
};
|
||||
|
||||
return versionInfo;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Tracer.WriteException(exception);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user