using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml; namespace SteamWare { /// /// Gestione update applicazioni /// public class UpdateMan { /// /// Init classe /// protected UpdateMan() { } /// /// Init classe /// public static UpdateMan obj = new UpdateMan(); /// /// Recupera l'ultima versione disponibile /// /// /// public UpdateInfoEventArgs getUpdateInfo(string remoteUrl) { UpdateInfoEventArgs args = new UpdateInfoEventArgs(); Version CurrentVersion; bool Mandatory; XmlDocument recXml = new XmlDocument(); try { // recupero dati HttpWebRequest request = (HttpWebRequest)HttpWebRequest.CreateHttp(remoteUrl); HttpWebResponse remData = (HttpWebResponse)request.GetResponse(); // converto in XML recXml.Load(remData.GetResponseStream()); XmlNodeList recData = recXml.SelectNodes("item"); if (recData != null) { foreach (XmlNode item in recData) { XmlNode appCastVersion = item.SelectSingleNode("version"); Version.TryParse(appCastVersion?.InnerText, out CurrentVersion); args.CurrentVersion = CurrentVersion; XmlNode appCastChangeLog = item.SelectSingleNode("changelog"); args.ChangelogURL = appCastChangeLog?.InnerText; XmlNode appCastUrl = item.SelectSingleNode("url"); args.DownloadURL = appCastUrl?.InnerText; XmlNode mandatory = item.SelectSingleNode("mandatory"); Boolean.TryParse(mandatory?.InnerText, out Mandatory); args.Mandatory = Mandatory; args.IsUpdateAvailable = true; } } } catch { // metto versione = 0 + errore... args.IsUpdateAvailable = false; args.CurrentVersion= new Version("0.0.0.0"); } return args; } /// /// Effettua download ultima versione applicativo /// /// /// /// public int downloadLatest(string remoteUrl, string localRepo, string packName) { int done = 0; // verifico directory if (!Directory.Exists(localRepo)) { Directory.CreateDirectory(localRepo); } string localFile = ""; string localLast = ""; try { // in primis scarico update info... UpdateInfoEventArgs updateData = getUpdateInfo(remoteUrl); if (updateData.IsUpdateAvailable) { string DownloadURL = updateData.DownloadURL; localFile = string.Format(@"{0}\{2}_Build_{1}.zip", localRepo, updateData.CurrentVersion, packName); localLast = string.Format(@"{0}\{1}.zip", localRepo, packName); // scarica file e salva in directory locale... var client = new WebClient(); client.DownloadFile(updateData.DownloadURL, localFile); // ora lo copio come latest... File.Copy(localFile, localLast, true); // indico come fatto! done = 1; } else { localFile = string.Format(@"{0}\Error.log", localRepo); // scrivo file in area target... using (StreamWriter sw = File.AppendText(localFile)) { sw.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss} ATTENZIONE! Installer NON disponibile | {1}", DateTime.Now, remoteUrl)); } } } catch (Exception exc) { localFile = string.Format(@"{0}\Error.log", localRepo); // scrivo file in area target... using (StreamWriter sw = File.AppendText(localFile)) { sw.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss} Errore in fase di download installers per {1}", DateTime.Now, remoteUrl)); sw.WriteLine(exc.ToString()); } } return done; } } /// /// Object of this class gives you all the details about the update useful in handling the update logic yourself. /// public class UpdateInfoEventArgs : EventArgs { /// /// If new update is available then returns true otherwise false. /// public bool IsUpdateAvailable { get; set; } /// /// Download URL of the update file. /// public string DownloadURL { get; set; } /// /// URL of the webpage specifying changes in the new update. /// public string ChangelogURL { get; set; } /// /// Returns newest version of the application available to download. /// public Version CurrentVersion { get; set; } /// /// Returns version of the application currently installed on the user's PC. /// public Version InstalledVersion { get; set; } /// /// Shows if the update is required or optional. /// public bool Mandatory { get; set; } } }