private static bool CheckForUpdate(UpdateInfoEventArgs args)
        {
            Assembly assembly = Assembly.GetEntryAssembly();

            args.InstalledVersion  = assembly.GetName().Version;
            args.IsUpdateAvailable = new Version(args.CurrentVersion) > args.InstalledVersion;
            return(args.IsUpdateAvailable);
        }
        /// <summary>
        /// Prüft anhand der XML - Datei, ob eine neue Version verfügbar ist
        /// </summary>
        /// <param name="xmlLink"></param> URL or path of the xml file that contains information about latest version of the application.
        public static async void Start(string xmlLink)
        {
            try
            {
                XmlLink = new Uri(xmlLink);
                UpdateInfoEventArgs updateInfo = null;
                if (XmlLink.Scheme.Equals(Uri.UriSchemeHttp) || XmlLink.Scheme.Equals(Uri.UriSchemeHttps))
                {
                    connectionType = new ConnectionTypeWeb();
                }
                else if (XmlLink.Scheme.Equals(Uri.UriSchemeFile))
                {
                    connectionType = new ConnectionTypeFile();
                }

                updateInfo = await connectionType.ReadXmlFileAsync(XmlLink);

                bool IsUpdateAvailable = CheckForUpdate(updateInfo);

                // TODO: Wollen Sie das Update durchführen?

                Uri downloadLink = new Uri(updateInfo.DownloadURL);
                if (downloadLink.Scheme.Equals(Uri.UriSchemeHttp) || downloadLink.Scheme.Equals(Uri.UriSchemeHttps))
                {
                    connectionType = new ConnectionTypeWeb();
                }
                else if (downloadLink.Scheme.Equals(Uri.UriSchemeFile))
                {
                    connectionType = new ConnectionTypeFile();
                }

                connectionType.DownloadProgressChanged += DownloadUpdateProgressChanged;

                await connectionType.StartDownloadAsync(updateInfo.DownloadURL, Path.GetFullPath(updateInfo.DownloadFileName));

                Console.WriteLine($"***** {updateInfo.DownloadURL} *****");
            }
            catch (Exception exception)
            {
                ShowError(exception);
            }
        }
        public async Task <UpdateInfoEventArgs> ReadXmlFileAsync(Uri xmlUri)
        {
            _xmlUri = xmlUri;
            HttpClient _httpClient = new HttpClient {
                Timeout = TimeSpan.FromDays(1)
            };
            string xmlString = await GetXmlString(_httpClient);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdateInfoEventArgs));
            XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlString))
            {
                XmlResolver = null
            };

            _updateInfo = (UpdateInfoEventArgs)xmlSerializer.Deserialize(xmlTextReader);
            Dispose(_httpClient);

            if (string.IsNullOrEmpty(_updateInfo.CurrentVersion) || string.IsNullOrEmpty(_updateInfo.DownloadURL))
            {
                throw new MissingFieldException();
            }

            return(_updateInfo);
        }
Exemplo n.º 4
0
        public async Task <UpdateInfoEventArgs> ReadXmlFileAsync(Uri xmlUri)
        {
            byte[] result;
            using (FileStream SourceStream = File.Open(xmlUri.LocalPath, FileMode.Open))
            {
                result = new byte[SourceStream.Length];
                await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
            }
            string        xmlString     = System.Text.Encoding.UTF8.GetString(result);
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(UpdateInfoEventArgs));
            XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlString))
            {
                XmlResolver = null
            };

            _updateInfo = (UpdateInfoEventArgs)xmlSerializer.Deserialize(xmlTextReader);

            if (string.IsNullOrEmpty(_updateInfo.CurrentVersion) || string.IsNullOrEmpty(_updateInfo.DownloadURL))
            {
                throw new MissingFieldException();
            }

            return(_updateInfo);
        }