主要流程:

  1. 使用HttpWebRequest抓取Github API網頁內容
  2. 使用正則表達式尋找版本號碼,正則表達式:[0-9]*\.[0-9]*\.[0-9_]*
  3. 使用if else逐一比對是否有重複版本號碼,有的話就跳出迴圈,比對下一個版本號碼
  4. 將版本號碼插入至下載連結

實際程式碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Get_WebPage_Data
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void Insert_Text(string content)
        {
            int count = textBox1.Text.Length;
            string get_box_text = textBox1.Text;
            textBox1.Text = get_box_text.Insert(count, content + "\r\n");
            //////自動卷軸到最底下
            textBox1.ScrollBars = ScrollBars.Vertical;
            textBox1.SelectionStart = textBox1.Text.Length;
            textBox1.ScrollToCaret();
        }
        
        private void Get_Page_Bt_Click(object sender, EventArgs e)
        {
            string strUrl = @"https://api.github.com/repos/q3aql/aria2-static-builds/tags";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Timeout = 10000;
            request.Method = "GET";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Windows NT 5.2; Windows NT 6.0; Windows NT 6.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; MS-RTC LM 8; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 4.0C; .NET CLR 4.0E)";
            HttpWebResponse webresponse = (HttpWebResponse)request.GetResponse();
            StreamReader streamReader = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8);
            string retString = streamReader.ReadToEnd();
            webresponse.Close();
            streamReader.Close();
            string Compare = "";
            foreach (Match match in Regex.Matches(retString, @"[0-9]*\.[0-9]*\.[0-9_]*"))
            {
                string Find_Result = match.Value.ToString();
                if(Find_Result == Compare)
                {}
                else
                {
                    Insert_Text("版本號碼:"+ Find_Result);
                    Insert_Text("下載連結:"+@" "+@"https://github.com/q3aql/aria2-static-builds/releases/download/v" + Find_Result + @"/aria2-" + Find_Result + @"-win-64bit-build1.7z");
                    Compare = Find_Result;
                }  
            }
        }
    }
}

運行結果:
運行結果
下載程式範例:
https://mega.nz/#!qPpUDa4b!ZYISnA-eVwhAe7YyBEcmX24OULiGHlja-OT0OmAlodk

Last modification:December 3, 2018
If you think my article is useful to you, please feel free to appreciate