47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
using Plane.Logging;
|
|
using System;
|
|
using System.Net;
|
|
|
|
namespace Plane.Windows
|
|
{
|
|
public class UpdateChecker
|
|
{
|
|
private ILogger _logger;
|
|
|
|
public UpdateChecker(string urlToCheckUpdate, ILogger logger)
|
|
{
|
|
this.Url = urlToCheckUpdate;
|
|
_logger = logger;
|
|
}
|
|
|
|
public Version LatestVersion { get; set; }
|
|
|
|
public string Url { get; set; }
|
|
|
|
public void CheckAsync(Action<Version> completedCallback)
|
|
{
|
|
using (var client = new WebClient())
|
|
{
|
|
client.DownloadStringCompleted += (s, e) =>
|
|
{
|
|
try
|
|
{
|
|
if (e.Result.Length > 128)
|
|
{
|
|
// 这么长,肯定是假的。
|
|
return;
|
|
}
|
|
LatestVersion = Version.Parse(e.Result.Split('_')[0]);
|
|
completedCallback?.Invoke(LatestVersion);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Log(ex);
|
|
}
|
|
};
|
|
client.DownloadStringAsync(new Uri(Url));
|
|
}
|
|
}
|
|
}
|
|
}
|