从 GitHub 文本文件中读取特定值

Reading a specific value from a GitHub text file

我想从 Internet 上的文本文件中读取某个单词的特定赋值。

在输出中"content"我得到了文本文件的完整内容。

但我只想要 v7.7.3 行:version = "v7.7.3"

如何使用流阅读器按版本过滤?

即LastVersion.txt文件:

[general]
version    = "v7.7.3"
messagenew = "Works with June 2018 Update!\n Plus new Smart Farm strategy\n New Siege Machines\n For more information, go to \n https://mybot.run \n Always free and open source."
messageold = "A new version of MyBot (v7.7.3) is available!\nPlease download the latest from:\nhttps://mybot.run"

更新:这是我当前的代码。

public string myBotNewVersionURL = "https://raw.githubusercontent.com/MyBotRun/MyBot/master/LastVersion.txt";
public string myBotDownloadURL = null;
public string userDownloadFolder = @"C:\Users\XXX\Download\";
public string newMyBotVersion = null;
public string currentMyBotVersion = null;
public string currentMyBotFileName = null;
public string currentMyBotPath = null;

public void Btn_checkUpdate_Click(object sender, EventArgs e)
{
    OpenFileDialog openCurrentMyBot = new OpenFileDialog();
        openCurrentMyBot.Title = "Choose MyBot.run.exe";
        openCurrentMyBot.Filter = "Application file|*.exe";
        openCurrentMyBot.InitialDirectory = userDownloadFolder;
        if (openCurrentMyBot.ShowDialog() == DialogResult.OK)
        {
            MyBot_set.SetValue("mybot_path", Path.GetDirectoryName(openCurrentMyBot.FileName));
            MyBot_set.SetValue("mybot_exe", Path.GetFullPath(openCurrentMyBot.FileName));
            string latestMyBotPath = Path.GetFullPath(openCurrentMyBot.FileName);
            var latestMyBotVersionInfo = FileVersionInfo.GetVersionInfo(latestMyBotPath);
            currentMyBotVersion = "v" + latestMyBotVersionInfo.FileVersion;

            MyBot_set.SetValue("mybot_version", currentMyBotVersion);
            WebClient myBotNewVersionClient = new WebClient();
            Stream stream = myBotNewVersionClient.OpenRead(myBotNewVersionURL);
            StreamReader reader = new StreamReader(stream);
            String content = reader.ReadToEnd();
            var sb = new StringBuilder(content.Length);
            foreach (char i in content)
            {
                if (i == '\n')
                {
                    sb.Append(Environment.NewLine);
                }
                else if (i != '\r' && i != '\t')
                    sb.Append(i);
            }
            content = sb.ToString();
            var vals = content.Split(
                                        new[] { Environment.NewLine },
                                        StringSplitOptions.None
                                    )
                        .SkipWhile(line => !line.StartsWith("[general]"))
                        .Skip(1)
                        .Take(1)
                        .Select(line => new
                        {
                            Key = line.Substring(0, line.IndexOf('=')),
                            Value = line.Substring(line.IndexOf('=') + 1).Replace("\"", "").Replace(" ", "")
                        });
            newMyBotVersion = vals.FirstOrDefault().Value;
}

从本地读取

  var vals = File.ReadLines("..\..\test.ini")
                    .SkipWhile(line => !line.StartsWith("[general]"))
                    .Skip(1)
                    .Take(1)
                    .Select(line => new
                     {
                         Key = line.Substring(0, line.IndexOf('=')),
                         Value = line.Substring(line.IndexOf('=') + 1)
                     });

    Console.WriteLine("Key : " + vals.FirstOrDefault().Key +
                      " Value : " + vals.FirstOrDefault().Value);

已更新

从 Git 读取,File.ReadLines 不适用于 URL。

string myBotNewVersionURL = "https://raw.githubusercontent.com/MyBotRun/MyBot/master/LastVersion.txt";

            WebClient myBotNewVersionClient = new WebClient();
            Stream stream = myBotNewVersionClient.OpenRead(myBotNewVersionURL);
            StreamReader reader = new StreamReader(stream);
            String content = reader.ReadToEnd();

            var sb = new StringBuilder(content.Length);
            foreach (char i in content)
            {
                if (i == '\n')
                {
                    sb.Append(Environment.NewLine);
                }
                else if (i != '\r' && i != '\t')
                    sb.Append(i);
            }

            content = sb.ToString();

            var vals = content.Split(
                                        new[] { Environment.NewLine },
                                        StringSplitOptions.None
                                    )
                        .SkipWhile(line => !line.StartsWith("[general]"))
                        .Skip(1)
                        .Take(1)
                        .Select(line => new
                        {
                            Key = line.Substring(0, line.IndexOf('=')),
                            Value = line.Substring(line.IndexOf('=') + 1)
                        });


            Console.WriteLine("Key : " + vals.FirstOrDefault().Key + " Value : " + vals.FirstOrDefault().Value);