/** * Call the GitHub REST API with / repos / Trinity-11 / FoenixIDE / releases. * From the returned JSON, check which one is the latest and if it matches ours. * */ private void CheckForUpdateToolStripMenuItem_Click(object sender, EventArgs e) { string URL = "https://api.github.com/repos/Trinity-11/FoenixIDE/releases"; HttpClient client = new HttpClient(); String version = AboutForm.AppVersion(); int appVersion = VersionValue(version); // Add an Accept header for JSON format client.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json"); client.DefaultRequestHeaders.Add("user-agent", "Foenix IDE"); bool done = false; // List data response. HttpResponseMessage response = client.GetAsync(URL).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs. if (response.IsSuccessStatusCode) { // Parse the response body. string value = response.Content.ReadAsStringAsync().Result; MatchCollection matches = Regex.Matches(value, "\"tag_name\":\"(.*?)\""); foreach (Match match in matches) { string fullRelease = match.Groups[1].Value; string release = fullRelease.Replace("release-", ""); int releaseVersion = VersionValue(release); if (releaseVersion > appVersion) { MessageBox.Show(string.Format("A new version is available.\nThe latest release is {0}, you are running version {1}.", release, version), "Version Check"); done = true; break; } } } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } if (!done) { MessageBox.Show("You are running the latest version of the Foenix IDE.", "Version Check"); } // Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous. client.Dispose(); }