Пример #1
0
        private void CheckForUpdates()
        {
            StatusLabel.Text = "Checking for updates...";
            string UpdatesFile = File.ReadAllText("..\\updates.json");
            if (UpdatesFile != null && UpdatesFile != string.Empty)
            {
                updates = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdatesObject>(UpdatesFile);
                if (updates == null)
                {
                    Done();
                    return;
                }

                using (wc = new WebClient())
                {
                    wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(this.DownloadProgressChanged);
                    wc.DownloadStringCompleted += new System.Net.DownloadStringCompletedEventHandler(this.DownloadStringAsynCompleted);

                    System.Uri uri = new System.Uri(updates.location + "updates.json");
                    wc.DownloadStringAsync(uri);
                }
            }
            else
            {
                Done();
            }
        }
Пример #2
0
        private void DownloadStringAsynCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                Done();
            }
            else if (e.Error != null)
            {
                Done();
            }
            else
            {
                string UpdatesFileFromWeb = e.Result;
                if (UpdatesFileFromWeb != null && UpdatesFileFromWeb != string.Empty)
                {
                    updatesFromWeb = Newtonsoft.Json.JsonConvert.DeserializeObject<UpdatesObject>(UpdatesFileFromWeb);
                    if (updatesFromWeb == null)
                    {
                        Done();
                        return;
                    }

                    // If a newer version is on the server.
                    if (updatesFromWeb.version != null &&
                        updates.version != null &&
                        updatesFromWeb.version > updates.version)
                    {
                        StatusLabel.Text = "Downloading update...";
                        try
                        {
                            wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(this.DownloadFileAsynCompleted);
                            System.Uri uri = new System.Uri(updates.location + updates.file);
                            wc.DownloadFileAsync(uri, "..\\update.zip");
                        }
                        catch (System.Net.WebException ex)
                        {
                            Console.WriteLine("Something went wrong while trying to download an update!");
                            Done();
                        }
                    }
                    else
                    {
                        Done();
                    }
                }
                else
                {
                    Done();
                }
            }
        }