/// <summary> /// Gets the latest release tag from Github. /// </summary> /// <returns>Returns an <see cref="UpdateCheck"/>.</returns> private static UpdateCheck GetLatestVersion() { // Somewhere to store the results UpdateCheck results = new UpdateCheck { CheckSuccess = false, }; try { // The API URL to check for the latest release // Returns a JSON payload containing all the details of the latest release string releasesUrl = "https://api.github.com/repos/benlye/flash-multi/releases/latest"; // Set TLS1.2, as required by the Github API System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // Create the WebRequest HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(releasesUrl); // Set the UserAgent, as required by the Github API webRequest.UserAgent = $"flash-multi-{Application.ProductVersion}"; // Disable keepalive so we don't hold a connection open webRequest.KeepAlive = false; // Get the response and read it using (WebResponse myResponse = webRequest.GetResponse()) using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8)) { // Read all of the output from the StreamReader string result = sr.ReadToEnd(); // Parse the release tag out of the JSON // This contains the version string int tagStart = result.IndexOf("\"tag_name\":"); int tagEnd = result.IndexOf(",", tagStart); string tagLine = result.Substring(tagStart, tagEnd - tagStart); string tag = tagLine.Split('"')[3]; // Add the release tag to the results results.LatestVersion = Version.Parse(tag); // Parse the release URL out of the JSON // This is the URL of the Github page containing details of the latest release int urlStart = result.IndexOf("\"html_url\":"); int urlEnd = result.IndexOf(",", urlStart); string urlLine = result.Substring(urlStart, urlEnd - urlStart); string url = urlLine.Split('"')[3]; // Add the release URL to the results results.ReleaseUrl = url; } // Define a regular expression to test the version number looks how we expect it to Regex versionRegex = new Regex(@"\d+\.\d+\.\d+"); // Check that the URL and version number are as we expect if (results.ReleaseUrl.StartsWith("https://github.com/benlye/flash-multi/releases/") && versionRegex.Match(results.LatestVersion.ToString()).Success) { // All looks good; the check succeeded Debug.WriteLine($"Update check succeeded. Latest version is {results.LatestVersion}"); results.CheckSuccess = true; } } catch (Exception ex) { Debug.WriteLine($"Error getting latest version: {ex.Message}"); } // Return the results return(results); }